问题描述
- java异常,具体描述如下,请问如何解决
-
程序代码如下:
package mytest;import java.net.URL;
import java.io.*;public class URLDemo {
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub URL addr = new URL("file://E:/Detect.txt"); //本地文本文件 InputStream is = addr.openStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is) ); while(true){ //从输入流不断读入数据,直到读完位置 String s = br.readLine(); if(null == s)break; System.out.println(s); } }
}
运行错误:
Exception in thread "main" java.net.UnknownHostException: E
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at mytest.URLDemo.main(URLDemo.java:11)
请问如何解决?
解决方案
执行了下你的代码,是file路径访问错误,去掉一个斜杠OK。修正如下:
URL addr = new URL("file:/E:/Detect.txt"); //本地文本文件
解决方案二:
Java异常如何解决?
解决方案三:
URL addr = new URL("file:///e:/test.txt"); //本地文本文件 --------少了一个/
解决方案四:
错误信息为:找不到主机,因为你用的是URL地址,URL的地址格式为:协议://主机:端口/资源路径(部分端口可以不写),改为:URL addr=new URL(“file://localhost/E:/...”);就可以了。
解决方案五:
感谢热心朋友们的回答
解决方案六:
file://E:/Detect.txt" 少了一个/