java.io.FileNotFoundException:(系统找不到指定的路径)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20743872/
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
java.io.FileNotFoundException: (The system cannot find the path specified)
提问by Indu
I am new to Socket programming. I am trying to write a file on server, but my code throws an exception:
我是 Socket 编程的新手。我试图在服务器上写一个文件,但我的代码抛出一个异常:
java.io.FileNotFoundException: Welcome-PC\IndiraSharing\hadoop.txt (The system cannot find the path specified)
java.io.FileNotFoundException: Welcome-PC\IndiraSharing\hadoop.txt (The system cannot find the path specified)
Here is my code.
这是我的代码。
Socket s;
ServerSocket server = new ServerSocket(5555);
String serveradd;
serveradd=s.getInetAddress().getHostName();
// System.out.println("Server accepted client");
InputStream input = s.getInputStream();
BufferedReader inReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String filename = inReader.readLine();
if ( !filename.equals("") ){
outReader.write("READY\n");
outReader.flush();
}
FileOutputStream wr = new FileOutputStream(new File(serveradd+"\IndiraSharing/" + filename));
byte[] buffer = new byte[s.getReceiveBufferSize()];
int bytesReceived = 0;
while((bytesReceived = input.read(buffer))>0)
{
wr.write(buffer,0,bytesReceived);
}
采纳答案by ???v?т?
The directory cannot be found. The message is confusing because it talks of "file" but in Java IO, a directory is a File too.
找不到目录。该消息令人困惑,因为它谈到“文件”,但在 Java IO 中,目录也是文件。
If you run:
如果你运行:
File dir = new File(serveradd+"\IndiraSharing")
System.out.println(dir.exists());
It will print:
它会打印:
false
You could try to create the directory structure:
您可以尝试创建目录结构:
dir.mkdirs();
Also, check the absolute path is what you are expecting:
另外,检查绝对路径是您所期望的:
System.out.println(dir.getAbsolutePath());
回答by tobe
Welcome-PC\IndiraSharing\hadoop.txt
is a relative path and I'm sure you should specify a absolute path for your socket program. BTW, the directory should exists even if you want to create a new file.
Welcome-PC\IndiraSharing\hadoop.txt
是一个相对路径,我确定你应该为你的套接字程序指定一个绝对路径。顺便说一句,即使您想创建一个新文件,该目录也应该存在。