0

I have a simple java project in intellij idea, I want to create a BufferedReader object from a local file in my project with local path to it, like this:

BufferedReader reader = new BufferedReader(new FileReader("/LOCAL/PATH/TO/PRIMES.TXT"));  

but I'm getting an error saying "no such file or directory" when I try with some local paths like:

/resources/primes.txt or  
/primes.txt or  
primes.txt

my file is located in src/resources/primes.txt
how should I fix this?

Mehdi Ijadnazar
  • 4,532
  • 4
  • 35
  • 35
  • The local path need to be absolute, something like `c:/foo/` (windows) or `/usr/share/foo` (linux). You most likely need a resource from classpath here. –  Jan 29 '14 at 08:52
  • see also http://stackoverflow.com/questions/15281428/java-relative-path-of-text-file-in-main –  Jan 29 '14 at 08:53

3 Answers3

0

Files location start from project home. For example if the file in you can access it as String fromFileName = "_ART7020.JPG";. So all path will be started from here in IntelliJ.

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
0

You could create a folder in your project, call it res or something like that. Put it in your project, not in src. Then when you want to access a txt file for example, you should do this :

new File("./res/TEXT.txt");
thetheodor
  • 248
  • 2
  • 22
0

you can use it like this:

import java.io.*;

public class test{
public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/localpath/localFile.txt")));
    String b=null;
    while ((b=br.readLine())!=null){
        System.out.println(b);
    }
}
}
Jeff.W
  • 1
  • 2