【问题标题】:Read csv file from website return java.io.FileNotFoundException从网站读取 csv 文件返回 java.io.FileNotFoundException
【发布时间】:2016-09-24 03:58:40
【问题描述】:

我正在尝试从 URL 读取 csv 文件。但我得到 java.io.FileNotFoundException。该 URL 可在浏览器上运行。请帮忙。 下面是我的csv阅读代码。

public static void main(String args[]) {


        InputStream input = null;

        try {
            input = new URL("http://prod2.riskval.com/site/table.csv​").openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Reader reader = new InputStreamReader(input, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String data;
            try {
                data = br.readLine();
                System.out.println(data);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

}

【问题讨论】:

    标签: java eclipse csv


    【解决方案1】:

    文件似乎是问题所在。我尝试使用另一个文件,它的工作原理是这样的。

    public static void main(String args[]) throws IOException {
    
    InputStream input = null;
    
    URL url = new URL("http://samplecsvs.s3.amazonaws.com/SalesJan2009.csv");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    
    String data;
    
    data = in.readLine();
    System.out.println(data);
    

    }

    【讨论】:

      【解决方案2】:

      原因是您的 URL 具有隐藏字符,我只是尝试在浏览器中复制粘贴相同的 URL,它给了我 404。根据this,当您从 MS Word 或写字板或类似编辑器中复制文本时,您的获取这些 %E2%80%8B 表示行尾的字符。

      只需将 URL 复制粘贴到 sublime 或任何其他显示隐藏字符的文本编辑器中,您应该会看到类似这样的内容

      http://prod2.riskval.com/site/table.csv%E2%80%8B

      【讨论】:

      • 非常感谢!将 UTF-8 更改为 MS950 后即可使用。
      • @KristenChang 如果对您有帮助,请接受答案。
      【解决方案3】:

      这应该可以解决您的阅读问题。请研究一下。

      package tttttttttttttttt;
      
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.net.MalformedURLException;
      import java.net.URL;
      
      public class ss {
          public static void main(String args[]) {
      
              InputStream input = null;
              BufferedReader br = null;
              StringBuilder body = new StringBuilder();
              String line = null;
              try {
                  input = new URL("http://www.google.com").openStream();
                  System.err.println(input);
                  System.err.println(input.available());
                  System.err.println(input.markSupported());
                  br = new BufferedReader(new InputStreamReader(input));
                  while ((line = br.readLine()) != null) {
                      System.out.println(line);
                  }
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              System.out.print("Captured " + br);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-01
        • 2014-12-28
        • 2013-07-12
        • 2020-11-17
        • 2014-02-23
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多