how to read text file relative path

后端 未结 4 2171
迷失自我
迷失自我 2021-02-20 01:57

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

4条回答
  •  余生分开走
    2021-02-20 02:06

    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();
    }                   
    

提交回复
热议问题