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
原创粉丝点击