Java工程资源文件路径设置
来源:互联网 发布:淘宝佣金是多少 编辑:程序博客网 时间:2024/05/01 14:31
Java程序在查找资源的相对路径时是从工程根目录下开始的。
下面是读取文本、图片和视频的介绍:
首先看Eclipse工程的架构,注意项目根目录下有个resources文件夹,里面是资源文件,是我们要读取的内容。
文本效果截图:
图片效果截图:
视频效果截图:
主类代码:
文本和图片资源的访问路径可以使相对路径也可以使绝对路径,我们把资源文件放在工程根目录下的resources文件夹(我们自己新建的文件夹,用于存放资源文件,当然也可以取其它名字,也可以建多级路径),由于Java程序对相对路径的资源是从根目录下开始查找的,所以相对路径的资源路径可以这样写:/resources/*,其中*是具体的资源名。如果用绝对路径,比如把资源文件放在E盘根目录下,那么资源路径可以这样写:file:/E:/*,其中*是具体的资源名。
对于视频文件来说,可以向前面介绍的那样使用绝对路径;如果要使用相对路径的话,有点特殊,如下图所示:
可以看到文件名为“hello.avi”视频位于工程根目录下,其文件路径为“file:./hello.avi”,依次类推。
package bupt.xujinliang.showimage;/** * * @author jin * */public class ResourcePathTest { public static void main(String[] args) { //String imagePath="adapterpattern.jpg";//能用 //String imagePath="src/resources/adapterpattern.jpg";//能用 String imagePath="resources/girl.jpg"; //String videoPath = "file:/resources/hello.avi";//不能用 //String videoPath = "resources/hello.avi";//不能用 String videoPath = "file:/E:/hello.avi";//能用 String txtPath = "resources/jin.txt";//可用 new ShowImage(imagePath); new PlayVideo(videoPath).play(); new ReadText(txtPath).readTxtFile(); } }
读取文本的类的代码:
package bupt.xujinliang.showimage;/** * @jin */import java.awt.Frame;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import javax.swing.JLabel;public class ReadText {private String filePath;private Frame f;private JLabel label;public ReadText(String filePath) {this.filePath = filePath;f = new Frame("读取文本");label = new JLabel("读取文本前");f.add(label);f.setSize(600, 100);f.setVisible(true);}public void readTxtFile(){try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); label.setText(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); }} catch (Exception e) {System.out.println("读取文件内容出错"); e.printStackTrace(); } }}读取图片的类的代码:
package bupt.xujinliang.showimage;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;/** * * @author jin * */public class ShowImage extends Frame{String filename; Graphics g; public ShowImage(String filename) { super("图片展示"); this.setSize(420,350); this.setVisible(true); this.filename = filename; } public void paint (Graphics g) { Image image = this.getToolkit().getImage(filename); g.drawImage(image, 0, 0, this); } }
读取视频的类的代码:
package bupt.xujinliang.showimage;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.Frame;import java.awt.Panel;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import javax.media.ControllerEvent;import javax.media.ControllerListener;import javax.media.EndOfMediaEvent;import javax.media.Manager;import javax.media.NoPlayerException;import javax.media.Player;import javax.media.PrefetchCompleteEvent;import javax.media.RealizeCompleteEvent;/** * * @author jin * */public class PlayVideo implements ControllerListener {String FilePath;String Title = "视频演示"; private Frame f; private Player player; private Component videoComponent; private Component controlComponent = null; private Panel panel; private int videoWidth = 0; private int videoHeight = 0; private int controlHeight = 30; private int insetWidth = 10; private int insetHeight = 30; public PlayVideo(String filePath) { this.FilePath = filePath; } public void play(){ f = new Frame(Title); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { if(player != null) { player.close(); } System.exit(0); } }); f.setSize(500,400); f.setVisible(true); URL url = null; try { url = new URL(FilePath); } catch (MalformedURLException e) { e.printStackTrace(); } try { player = Manager.createPlayer(url); } catch (NoPlayerException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } player.addControllerListener(this); player.realize(); } public void controllerUpdate(ControllerEvent ce) { if (ce instanceof RealizeCompleteEvent) { //player实例化完成后进行player播放前预处理 player.prefetch(); } else if (ce instanceof PrefetchCompleteEvent) { if (videoComponent != null) return; if ((videoComponent = player.getVisualComponent()) != null) { Dimension size = videoComponent.getPreferredSize(); videoWidth = size.width; videoHeight = size.height; f.add(videoComponent); } else { videoWidth = 320; } if ((controlComponent = player.getControlPanelComponent()) != null) { controlHeight = controlComponent.getPreferredSize().height; f.add(controlComponent, BorderLayout.SOUTH); } f.setSize(videoWidth + insetWidth, videoHeight + controlHeight + insetHeight); f.validate(); player.start(); } else if (ce instanceof EndOfMediaEvent) { player.stop(); } }}
1 0
- Java工程资源文件路径设置
- java普通类下设置资源文件相对路径
- Web、Java源代码、资源文件在Maven工程中的文件路径
- Web、Java源代码、资源文件在Maven工程中的文件路径
- java定位资源文件路径
- java 资源文件加载路径
- 解决java工程打包后无法找到资源文件路径的方法
- eclipse中java工程打包成jar无法找到src资源文件路径的解决办法
- java工程里CSS背景图片路径设置
- java工程里CSS背景图片路径设置
- java 文件 IO 设置路径
- java工程内部相对路径引用文件
- java中找到资源文件的路径
- Java 资源文件路径详解(getResource)
- Java 资源文件路径详解(getResource)
- Java 资源文件路径详解(getResource)
- JAVA获取资源路径、创建文件对象
- Java 资源文件路径详解(getResource)
- longene-wine-qq
- JAVA微信开发教程入门篇 -01 开发环境搭建
- Oracle SQL Developer 1.5.5设置关键字小写
- 二分查找法的实现和应用汇总
- 网站的基础:网站空间
- Java工程资源文件路径设置
- android字体大小根据分辨率自动调整
- Java 生产者消费者问题
- 转载:oracle: 浅谈sqlnet.ora文件的作用,及SQLNET.AUTHENTICATION_SERVICES设置
- Struts,JSP和XML解析三个问题
- valgrind-运行时诊断工具Memory Leak Check Tool
- •Web 服务器上的请求筛选被配置为拒绝该请求,因为查询字符串过长。
- 多线程GCD的使用