Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.io.FileNotFoundException_Java_Exception - Fatal编程技术网

java.io.FileNotFoundException

java.io.FileNotFoundException,java,exception,Java,Exception,我正试图从网上搜集一些数据,以便在我的应用程序中使用它 我试图从yahoo网站上获取数据,但当它试图以流式传输数据时,我得到了一个FileNotFoundException 我还明确设置了IP地址和端口 如果有人能告诉我哪里出了问题,我会非常感激的 我也发布了示例代码 parentUrl = "http://www.yahoo.com"; pageUrl = new URL(parentUrl); System.out.println(parentUrl); try { in = ne

我正试图从网上搜集一些数据,以便在我的应用程序中使用它

我试图从yahoo网站上获取数据,但当它试图以流式传输数据时,我得到了一个FileNotFoundException

我还明确设置了IP地址和端口

如果有人能告诉我哪里出了问题,我会非常感激的

我也发布了示例代码

parentUrl = "http://www.yahoo.com";
pageUrl = new URL(parentUrl);
System.out.println(parentUrl);

try {
    in = new BufferedReader(new InputStreamReader(pageUrl.openStream()));
} catch(Exception ex2) {
    ex2.printStackTrace();
}

while ((inputLine = in.readLine()) != null) {
    out.write(inputLine);
    in.close();
}

out.close();    

问题在于初始化
out
。您尚未向我们展示该代码,但它将类似于:

OutputStream out = new FileOutputStream("non/existent/path/somefilename");
这可能是因为您使用了相对路径,因此为了帮助您调试它,我建议您将其更改为:

File file = new File("non/existent/path/somefilename");
System.out.println(file.getAbsolutePath()); // start with this simple debugging
OutputStream out = new FileOutputStream(file);

我的猜测是文件的路径不是您认为的路径。

问题在于
out
的初始化。您尚未向我们展示该代码,但它将类似于:

OutputStream out = new FileOutputStream("non/existent/path/somefilename");
这可能是因为您使用了相对路径,因此为了帮助您调试它,我建议您将其更改为:

File file = new File("non/existent/path/somefilename");
System.out.println(file.getAbsolutePath()); // start with this simple debugging
OutputStream out = new FileOutputStream(file);

我的猜测是,文件的路径不是您认为的路径。

您需要显示构建
out
的代码。这就是问题所在。还要注意,您正在读取循环中调用.close()。你永远不能用这句话读超过一行。@CameronSkinner是对的。。您只需从
while
循环中删除
in.close()
。@CameronSkinner-我删除了该语句并将其推到循环外,它仍然会给我相同的异常。它将在所需位置创建一个文件test.html,但其中没有数据。如果您向我们显示实际的异常StackTrace,则会有所帮助。您需要显示构建
out
的代码。这就是问题所在。还要注意,您正在读取循环中调用.close()。你永远不能用这句话读超过一行。@CameronSkinner是对的。。您只需从
while
循环中删除
in.close()
。@CameronSkinner-我删除了该语句并将其推到循环外,它仍然会给我相同的异常。它将在所需位置创建一个文件test.html,但其中没有数据。如果您向我们展示实际的异常堆栈跟踪,这将有所帮助