如何从资源目录文件夹内的.txt文件读取?

时间:2020-07-26 18:36:23

标签: java android xml android-studio fileinputstream

这就是我需要做的:我有一个文本文件,其中包含一些我需要读取的值(假设它是example.txt)。我需要像使用ex一样读取该文件。 FileInputStreamBufferedReader)。我该怎么做呢?

PS-This is what I tried doing,但没有帮助。我总是会收到错误消息,说文件必须是.xml

2 个答案:

答案 0 :(得分:0)

try {
        BufferedReader bufferReader = new BufferedReader(new FileReader(file);
        try {
            String line = bufferReader.readLine();
            while(line != null) { 
            //do something
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
         e.printStackTrace();
    }

答案 1 :(得分:-1)

try {
    FileInputStream fileInputStream = new FileInputStream("./src/main/resources/example.txt");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

visit this image