1

I want to make possible that any file inside my project can be readable by any user, considering that they wont have the same directory as me.

So i have this:

String filme = "somefile.txt"

String datastr = "";
   reader = new BufferedReader( new FileReader("C:/Path"+filename));
    String line = reader.readLine();
    while( line != null) {
        datastr += line + "\n";
        line = reader.readLine();
    }

Any help would be appreciated. Thanks in advance

1ac0
  • 2,875
  • 3
  • 33
  • 47
pMpC
  • 257
  • 3
  • 14

1 Answers1

0

Just to be simple, ClassLoader is answer:

ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL rsc = cl.getResource("MyFile.txt");
File f = new File(rsc.getPath());

But here are more answers on this subject here, here and here just for quick search. Use search! ;-)

Community
  • 1
  • 1
1ac0
  • 2,875
  • 3
  • 33
  • 47
  • So i did it like this... String datastr = ""; ClassLoader cl = Thread.currentThread().getContextClassLoader(); URL rsc = cl.getResource(file); FileReader f = new FileReader(rsc.getPath()); reader = new BufferedReader(f); String line = reader.readLine(); while( line != null) { datastr += line + "\n"; line = reader.readLine(); } and received the java.lang.NullPointerException... – pMpC Nov 03 '14 at 21:40
  • @pmpc use debugger to find-out in which step you get NPE. and what is in your `file` variable? – 1ac0 Nov 04 '14 at 07:34