13

I have read sources here & there, but did not get the following code working. Basically, I wish to read a text file, named 'Administrator' from the folder 'src'. I will need a relative path, since this project may be transferred to another person. Please be patient with me.

public void staffExists () throws IOException
    {               
        //http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java
        BufferedReader reader = new BufferedReader(new FileReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt")));

        try
        {               
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                if (!(line.startsWith("*")))
                {
                    System.out.println(line);
                }
            }

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }               

        finally
        {
            reader.close();
        }           
    }
Damodaran
  • 9,472
  • 8
  • 55
  • 78
user2945412
  • 297
  • 1
  • 3
  • 12
  • 2
    possible duplicate of [How to read text file from relative path in a project?](http://stackoverflow.com/questions/3844307/how-to-read-text-file-from-relative-path-in-a-project) – Kas Nov 09 '13 at 08:45
  • @Dooby Inc: have looked at the suggested url. but I cannot get it working still, please kindly guide me. – user2945412 Nov 09 '13 at 08:48
  • @ Little Child: I got this persistent error io.filenotfoundexception – user2945412 Nov 09 '13 at 08:49
  • The first line under the method signature should go inside the try block, nothing else in there cause throws an exception. – Paul Samsotha Nov 09 '13 at 08:51
  • Use double slashes in path specification. "//DBTextFiles//Administrator.txt" – Dejan Nov 09 '13 at 08:51
  • wats the exception you facinf – constantlearner Nov 09 '13 at 08:51
  • @Little Child , Please make sure your application have Read access to the Directory or to the file , And Your file path is possibly invalid that's why you are getting a FileNotFound exception , – Kas Nov 09 '13 at 08:52
  • @DjDexter5GHz double slashes is not the correct syntax for a relative file path... in fact I don't know what it would be correct for in Java, probably nothing. – Robin Green Nov 09 '13 at 08:52
  • @DoobyInc LOL ! I am not the OP here :D – An SO User Nov 09 '13 at 08:54
  • yeah, i just googled that, and you are right. I had similar problems like this guy, but i forgot how to solve it. I think this below should do the trick. :) – Dejan Nov 09 '13 at 08:55
  • @little child , Sorry for the mistake buddy – Kas Nov 09 '13 at 08:57
  • Now i get more confused with the many answers. which approach do I take then? – user2945412 Nov 09 '13 at 09:02
  • Just wrote a solution here, hope it helps http://stackoverflow.com/questions/8638582/java-relative-file-paths – Kevin Lee Mar 11 '14 at 14:23

4 Answers4

23

This is a valid absolute path (on the systems I'm aware of):

    /path/to/directory/../../otherfolder/etc/

So what the other answer was saying, was to get the path to the current directory with:

    String filePath = new File("").getAbsolutePath();

Then concatenate your relative path with:

    filePath.concat("path to the property file");
Community
  • 1
  • 1
willy
  • 1,374
  • 10
  • 10
11

Now I get it, somewhat answers here & there do help in getting me to the goal. Did a short edit to my code & it worked. Hope it'll also help some poor souls out there.

String filePath = new File("").getAbsolutePath();
System.out.println (filePath);

//http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java    
//http://stackoverflow.com/questions/19874066/how-to-read-text-file-relative-path
BufferedReader reader = new BufferedReader(new FileReader(filePath + "/src/DBTextFiles/Administrator.txt"));

try
{                           
    String line = null;         
    while ((line = reader.readLine()) != null)
    {
        if (!(line.startsWith("*")))
        {
            System.out.println(line);
        }
    }               
}
catch (IOException ex)
{
    ex.printStackTrace();
}               

finally
{
    reader.close();
}                   
Alex Weitz
  • 2,451
  • 2
  • 24
  • 41
user2945412
  • 297
  • 1
  • 3
  • 12
1

This is not correct:

new FileReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt"))

You want:

new InputStreamReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt"))
Robin Green
  • 29,408
  • 13
  • 94
  • 178
0

In almost all cases you should use the portable forward slash "/"." In every case you should use either the File constructor that accepts a File (parent) & String (file name) or use System.getProperty("file.separator").

Kas
  • 3,209
  • 3
  • 22
  • 51