【问题标题】:Why do I keep getting an I/O exception?为什么我不断收到 I/O 异常?
【发布时间】:2013-03-22 06:19:35
【问题描述】:

我创建了代码来访问我下载的文件,但这不是作业想要的。它希望我使用此处给出的 URL 访问信息。我不确定为什么我不断收到 IO 异常。

public static void main(String[] args) throws Exception{

    int test = 0;
    int sum = 0;
    int i = 0;
    try{
    java.net.URL url = new java.net.URL("http://cs.armstrong.edu/liang/data/Scores.txt");
    Scanner input = new Scanner(url.openStream());
    while (input.hasNext()){
        int score = input.nextInt();
        sum += score;
        i++;
    }
    }
    catch(java.net.MalformedURLException ex){
        ex.printStackTrace();
    }
    catch(java.io.IOException ex){
    System.out.println("I/O Errors: no such file");
    }


    int avg = sum / i;
    System.out.println("The average is: " + avg);
    System.out.println("The sum is: " + sum);




}

堆栈跟踪

java.net.SocketException: Invalid argument: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at readfile.main(readfile.java:15)

线程“主”java.lang.ArithmeticException 中的异常:/ 为零 在 readfile.main(readfile.java:30)

【问题讨论】:

  • 你能从浏览器中点击网址吗?
  • 你能打印出那个异常的堆栈跟踪吗?
  • 我可以从浏览器中访问 URL。
  • 你不应该捕获一个 io 异常并打印它是用于“找不到文件”而不检查它的实际用途。
  • 我尝试运行您的代码,它对我来说效果很好。您很可能需要设置代理设置。

标签: java exception io


【解决方案1】:

代码完全没问题。只需在连接前添加以下部分以设置代理详细信息。

//The follwing settings is needed for Connecting to internet from Local Network
System.getProperties().put("http.proxyHost", "ip.add.of.proxy");
System.getProperties().put("http.proxyPort", "80");
System.getProperties().put("http.proxyUser", "user_name"); // Your username
System.getProperties().put("http.proxyPassword", "*************"); // your password

并且还将下面的代码移动到 try catch 块中,否则如果连接失败,您将收到divide by zero 异常。

int avg = sum / i;
System.out.println("The average is: " + avg);
System.out.println("The sum is: " + sum);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2013-11-08
    相关资源
    最近更新 更多