Java I';我一直在努力弄清楚如何正确地路径我的文件输入。I';我正在使用Eclipse
异常为java.io.FileNotFoundException。我尝试过多种方法,甚至尝试过将其粘贴到几乎所有地方。我想我可能会使用文件输入流,但到目前为止我还是迷路了。我感谢你的解释。谢谢该程序的目标是解析一个包含学生姓名和成绩的文件,然后将其组织成中位数、最好成绩和最差成绩,同时将其作为单个文件输出Java I';我一直在努力弄清楚如何正确地路径我的文件输入。I';我正在使用Eclipse,java,eclipse,file-io,bufferedreader,Java,Eclipse,File Io,Bufferedreader,异常为java.io.FileNotFoundException。我尝试过多种方法,甚至尝试过将其粘贴到几乎所有地方。我想我可能会使用文件输入流,但到目前为止我还是迷路了。我感谢你的解释。谢谢该程序的目标是解析一个包含学生姓名和成绩的文件,然后将其组织成中位数、最好成绩和最差成绩,同时将其作为单个文件输出 //String path = "C:/Users/Rob/Documents/"; FileReader parseFile = new FileReader("input.
//String path = "C:/Users/Rob/Documents/";
FileReader parseFile = new FileReader("input.txt");
BufferedReader parseText = new BufferedReader(parseFile);
嘿Robbie我不是专家但告诉我这是否有效
FileReader parseFile = new FileReader("C:/Users/Rob/Documents/input.txt");
如果您没有注意到XD,您也会注释掉字符串路径。
祝你好运,朋友 如果您的文件始终保持在同一位置,您可以使用上面的答案,但如果它放在不同的操作系统(例如Unix)上,/字符将无法按预期工作。因此,我会使用
FileReader parseFile = new FileReader(new File("input.txt").toAbsolutePath());
此代码假定input.txt与应用程序位于同一目录中。如果不是,你就不能用这个
如果你打算用它来分析学生的成绩,我建议不要完全使用FileReader或BufferedReader。NIO有一个readAllLines(URI)代码>返回列表的函数
:
您可以使用构造函数文件(字符串文件路径)
获得文件
对象
希望这能回答你的问题
List<String> lines = Files.readAllLines(new File("input.txt").toAbsoluteFile().toURI());
public static String debugConnectionsToFile(File file) {
if(!file.exists()){
return "file does not exist!";
}
else if(!file.isFile()){
return "File is not actually a file!";
}
else if(!file.canRead()){
return "File cannot be read!";
}
else{
try {
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
try {
br.readLine();
br.close(); //It is true that this statement could cause an error, but it has never happened to me before.
} catch (IOException e) {
return "File cannot be read by the reader!";
}
} catch (FileNotFoundException e) {
return "File cannot be found or accessed by the reader";
}
return "It works fine!";
}
}