开发者

java reading text from src without writing a long path

开发者 https://www.devze.com 2023-04-03 15:38 出处:网络
I can read texts and write them to console however when i install this application to another computer wherever it is installed I dont want to change the path of the txt file.开发者_JS百科 I want to w

I can read texts and write them to console however when i install this application to another computer wherever it is installed I dont want to change the path of the txt file.开发者_JS百科 I want to write it like

BufferedReader in = new BufferedReader(new FileReader("xxx.txt"));

I don't want to:

BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\abcde\\Desktop\\xxx.txt"));

is there any way to show this txt file? By the way I put this txt file inside the sources but it cant read!


First get the default application path then check if file exist if exist continue if not close application.

 String path = System.getProperty("user.dir");
      System.out.println(path + "\\disSoruCevap.txt");
      File file = new File(path + "\\disSoruCevap.txt");


      if (!file.exists()) {
          System.out.println("System couldnt file source file!");
          System.out.println("Application will explode");
      }

EDIT*

Please prefer one of the answer using resource streams, as you will see from comments using user.dir is not safe in every case.


You are looking for :

BufferedReader in = new BufferedReader(getClass().getResourceAsStream("/xxx.txt"));

This will load xxx.txt from your jar file (or any jar file in your class path that has that file inside its root directory).


URL fileURL= yourClassName.class.getResource("yourFileName.extension");
String myURL= fileURL.toString();

now you don't need long path name PLUS this one is dynamic in nature i.e., you can now move your project to any pc, any drive.
This is because it access URL by using your CLASS location not by any static location (like c:\folder\ab.mp3, then you can't access that file if you move to D drive because then you have to change to D:/folder/ab.mp3 manually which is static in nature)
(NOTE: just keep that file with your project)

You can use fileURL as: File file=new File(fileURL.toURI());
You can use myURL as: Media musicFile=new Media(myURL); //in javaFX which need string not url of file


    InputStream input = Class_name.class.getResourceAsStream("/xxx.txt");
    InputStreamReader inputReader = new InputStreamReader(input);

    BufferedReader br = new BufferedReader(inputReader);
    String line = null;
    try {
        while((line = br.readLine())!=null){
            System.out.println(line);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

You don't need to write or mention long path. Using this code Class_name.class.getResourceAsStream("/xxx.txt"), you can easily get your file.


BufferedReader in = new BufferedReader(new FileReader("xxx.txt")); works fine because when you run your application on an IDE, xxx.txt apparantly is lying in Java's working directory. Working directory is an operating system feature and it can not be changed. There are a few ways to deal with this.

1 - use file constructor new File(parent, filename); and load parent using a public static final constant or a property (either passed from command line or otherwise)

2 - or use InputStream in = YourClass.class.getClassLoader().getResourceAsStream("xxx.txt"); - provided your xxx.txt file is packaged under same location as YourClass


Try:

InputStream is = ClassLoader.getSystemResourceAsStream("xxx.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));

Depending on where exactly is your file compared to the root of your classpath, you may have to replace xxx.txt3 with /xxx.txt.


My file paths are like this:

public final static String COURSE_FILE_LOCATION = "src/main/resources/courses.csv";

    public final static String PREREQUISITE_FILE_LOCATION = "src/main/resources/prerequisites.csv";

This doesn't work. So I delete the .iml file, .idea and target folder from the project and reload them.

Read the correct path like this:

java reading text from src without writing a long path

This would work then.

0

精彩评论

暂无评论...
验证码 换一张
取 消