1

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. 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!

Mat
  • 202,337
  • 40
  • 393
  • 406
albatross
  • 455
  • 2
  • 8
  • 27
  • 1
    Read the file from classpath. http://stackoverflow.com/questions/4821643/loading-a-configuration-file-from-the-classpath – adarshr Sep 06 '11 at 20:59
  • When you install the application on the other machine, is the file still there? Where is it, compared with the class files? – Jon Skeet Sep 06 '11 at 22:01
  • Look at this question - http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java – ring bearer Sep 06 '11 at 22:06
  • Please do not repeat the same question just because you aren't getting suitable answers. – Kev Sep 06 '11 at 23:16

7 Answers7

3

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.

HRgiger
  • 2,750
  • 26
  • 37
  • ``user.dir`` comtains the current working directory if the program was launched using a CLI. If not, it depends in the launcher: some will bring the folder containing the JAR, some the home directory of the user. Plus, I could totally launch the program using a CLI and, be in the "wrong" directory. What I'm trying to say is, don't use ``user.dir`` to try to locate the application root path. – Vivien Barousse Sep 06 '11 at 22:48
1
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

G33K_C0D3R
  • 67
  • 1
  • 9
1
    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.

Arif Rafsan
  • 91
  • 1
  • 1
  • 8
1

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).

Simone Gianni
  • 11,426
  • 40
  • 49
1

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

ring bearer
  • 20,383
  • 7
  • 59
  • 72
0

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:

enter image description here

This would work then.

Arefe
  • 11,321
  • 18
  • 114
  • 168
0

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.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118