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

huangapple 未分类评论43阅读模式
英文:

How do I read from a .txt file inside of a resource directory folder?

问题

以下是我需要发生的事情:我有一个包含一些值的文本文件,我需要读取这些值(假设文件名为 example.txt)。我需要像使用 FileInputStreamBufferedReader 一样读取该文件。我该如何进行?

PS - 这是我尝试过的方法,但没有帮助。我总是会收到一个错误,指示文件必须是 .xml 格式。

英文:

Here's what I need to happen: I've got a text file with some values in them that I need to read (Let's say it is example.txt). I need to read that file like i would using ex. FileInputStream or BufferedReader). How would I go about doing it?

PS - This is what I tried doing, but it didn't help. I would always get an error saying the file has to be .xml

答案1

得分: 0

尝试 {
    BufferedReader bufferReader = new BufferedReader(new FileReader(file));
    尝试 {
        String line = bufferReader.readLine();
(line != null) {
            // 做一些事情
        }
    } 捕获 (IOException e) {
        e.printStackTrace();
    }
} 捕获 (FileNotFoundException e) {
    e.printStackTrace();
}
英文:
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();
    }

huangapple
  • 本文由 发表于 2020年7月27日 02:36:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/63104165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定