Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put a textfile I want to use in eclipse?

I need to read a text file when I start my program. I'm using eclipse and started a new java project. In my project folder I got the "src" folder and the standard "JRE System Library" + staedteliste.txt... I just don't know where to put the text file. I literally tried every folder I could think off....I cannot use a "hard coded" path because the text file needs to be included with my app...

I use the following code to read the file, but I get this error:

Error:java.io.FileNotFoundException:staedteliste.txt(No such file or directory) 

public class Test {  ArrayList<String[]> values;  public static void main(String[] args) {     // TODO Auto-generated method stub      URL url = Test.class.getClassLoader().getResource("src/mjb/staedteliste.txt");     System.out.println(url.getPath()); // I get a nullpointerexception here!     loadList(); }  public static void loadList() {     BufferedReader reader;     String zeile = null;      try {         reader = new BufferedReader(new FileReader("src/mjb/staedteliste.txt"));         zeile = reader.readLine();                    ArrayList<String[]> values = new ArrayList<String[]>();          while (zeile != null) {                          values.add(zeile.split(";"));             zeile = reader.readLine();         }         System.out.println(values.size());         System.out.println(zeile);      } catch (IOException e) {         System.err.println("Error :"+e);     } } 

}

like image 507
MJB Avatar asked May 17 '10 16:05

MJB


People also ask

How do I open a Notepad file in Eclipse?

If you right-click the file in the Package Explorer/Navigator, you will see an Open-With menu item where you can select how the file should be opened. So, if you need to edit an . sh file just right-click and select Open With > Text Editor .

How do I access a file in Eclipse?

in Eclipse, right-click on your project and select "Properties". With "Resource" selected, it shows "Location:" which is the path to your project. Let's say if you added a folder to your project called Audio, then you could access a mp file like this: new File(".


2 Answers

Ask first yourself: Is your file an internal component of your application? (That usually implies that it's packed inside your JAR, or WAR if it is a web-app; typically, it's some configuration file or static resource, read-only).

If the answer is yes, you don't want to specify an absolute path for the file. But you neither want to access it with a relative path (as your example), because Java assumes that path is relative to the "current directory". Usually the preferred way for this scenario is to load it relatively from the classpath.

Java provides you the classLoader.getResource() method for doing this. And Eclipse (in the normal setup) assumes src/ is to be in the root of your classpath, so that, after compiling, it copies everything to your output directory ( bin/ ), the java files in compiled form ( .class ), the rest as is.

So, for example, if you place your file in src/Files/myfile.txt, it will be copied at compile time to bin/Files/myfile.txt ; and, at runtime, bin/ will be in (the root of) your classpath. So, by calling getResource("/Files/myfile.txt") (in some of its variants) you will be able to read it.

Edited: Further, if your file is conceptually tied to a java class (eg, some com.example.MyClass has a MyClass.cfg associated configuration file), you can use the getResource() method from the class and use a (resource) relative path: MyClass.getResource("MyClass.cfg"). The file then will be searched in the classpath, but with the class package pre-appended. So that, in this scenario, you'll typically place your MyClass.cfg and MyClass.java files in the same directory.

like image 137
leonbloy Avatar answered Oct 13 '22 05:10

leonbloy


One path to take is to

  1. Add the file you're working with to the classpath
  2. Use the resource loader to locate the file:

        URL url = Test.class.getClassLoader().getResource("myfile.txt");     System.out.println(url.getPath());     ... 
  3. Open it
like image 37
Greg Adamski Avatar answered Oct 13 '22 05:10

Greg Adamski