如何读取文本文件相对路径
2022-09-04 06:08:40
						我已经在这里和那里阅读了源代码,但没有让下面的代码工作。基本上,我希望从文件夹“src”中读取一个名为“管理员”的文本文件。我需要一个相对的路径,因为这个项目可能会被转移到另一个人身上。请耐心等待我。
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();
        }           
    }