如何解决java.nio.file.NoSuchFileException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48076063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve the java.nio.file.NoSuchFileException?
提问by Snkini
I have a file called "result.csv", from that file i want to read certain data and display them. I have that file in my eclipse project folder itself. Still i'm unable to read the file.
我有一个名为“result.csv”的文件,我想从该文件中读取某些数据并显示它们。我的 eclipse 项目文件夹中有该文件。我仍然无法读取文件。
public static void main(String [] args) {
int i=0;
String filename="result.csv";
Path pathToFile = Paths.get(filename);
try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {
// read the first line from the text file
String line = br.readLine();
// loop until all lines are read
while (i<10) {
// use string.split to load a string array with the values from
// each line of
// the file, using a comma as the delimiter
String[] attributes = line.split(",");
double x=Double.parseDouble(attributes[8]);
double y=Double.parseDouble(attributes[9]);
System.out.println(GeoHash.withCharacterPrecision(x, y, 10));
// read next line before looping
// if end of file reached, line would be null
line = br.readLine();
i++;
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
OUTPUT:
输出:
java.nio.file.NoSuchFileException: result.csv
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
at java.nio.file.Files.newInputStream(Unknown Source)
at java.nio.file.Files.newBufferedReader(Unknown Source)
at com.uvce.cse.searchiot.geohash.TestGeoHash.main(TestGeoHash.java:19)
Can anyone point where exactly i missed? and how can i overcome this or any alternate methods for this method?
谁能指出我到底错过了什么?我怎样才能克服这种方法或这种方法的任何替代方法?
采纳答案by DodgyCodeException
The problem is that your default directory at application startup is not what you think it is. Try adding the following line to your code, just after you create the path:
问题是您在应用程序启动时的默认目录不是您认为的那样。在创建路径之后,尝试将以下行添加到您的代码中:
public static void main(String [] args) {
int i=0;
String filename="result.csv";
Path pathToFile = Paths.get(filename);
System.out.println(pathToFile.toAbsolutePath());
That way, you'll see exactly where it is looking for the file.
这样,您将准确地看到它正在寻找文件的位置。
How to fix it is your decision. You can use a full path spec instead of just a filename, or put the filename in a special "Resources" directory and reference it using a relative path, or move the file to wherever your default directory is.
如何修复它是您的决定。您可以使用完整路径规范而不仅仅是文件名,或者将文件名放在特殊的“资源”目录中并使用相对路径引用它,或者将文件移动到默认目录所在的任何位置。
回答by Alex Cuadrón
The problem there is that java isn't able to find the "result.csv" file in the project folder. Thus, try to use the fully qualified path to the file, e.g. C:\your_folder\project\result.csv
in the Path variable. Also I think It would be better to use bufferedreader like this: BufferedReader br = new BufferedReader(new FileReader(insert here the String in which is defined the path to the file));
Check the uses of BufferedReader here
问题是java无法在项目文件夹中找到“result.csv”文件。因此,尝试使用文件的完全限定路径,例如C:\your_folder\project\result.csv
在 Path 变量中。另外我认为像这样使用bufferedreader会更好:BufferedReader br = new BufferedReader(new FileReader(insert here the String in which is defined the path to the file));
在这里检查BufferedReader的使用
回答by u9154970
If your file("result.csv")
in the src directory, you should use the "src/result.csv"instead of "result.csv".
如果您file("result.csv")
在 src 目录中,则应使用"src/result.csv"而不是"result.csv"。
回答by Nimesh Bumb
If you're a MacOSX user please type the file path manually instead of copying it from "Get Info". You'll get something like this if you copied it from "Get Info": /Users/username<200e><2068><2068>/Desktop<2069>/source.txt
如果您是 MacOSX 用户,请手动输入文件路径,而不是从“获取信息”中复制。如果你从“获取信息”中复制它,你会得到这样的东西: /Users/username<200e><2068><2068>/Desktop<2069>/source.txt