gpt4 book ai didi

java - 打开文本文件时遇到问题

转载 作者:行者123 更新时间:2023-12-01 11:30:39 25 4
gpt4 key购买 nike

我在读取文件时遇到一些问题。我的 input.txt 在内容包中,但程序仍然无法打开该文件。

String line = null; 
try{
//loen faili
FileReader fileReader = new FileReader("contents/input.txt");
BufferedReader buffReader = new BufferedReader(fileReader);
while((line = buffReader.readLine()) != null){
System.out.println(line);
}
buffReader.close();
}catch(FileNotFoundException ex){
System.out.println("Error opening file");
}catch(IOException ex){
System.out.println("Error reading file");
}

最佳答案

由于您需要以不同的方式打开应用程序包中的文件与磁盘中的文件,请尝试:

InputStream  is = getClass().getClassLoader().getResourceAsStream("contents/input.txt");

关于java - 打开文本文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com