您如何读取下载到浏览器中的CSV文件的内容



我需要使用Java读取托管CSV文件的内容。击中CSV托管的此URL将文件下载到浏览器中。我如何访问此文件并阅读其内容,而无需在本地执行任何操作?

目前我有:

    try {
        URL url = new URL("URL here");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        CsvReader products = new CsvReader(in);
        products.readHeaders();
        while (products.readRecord()) {
            products.get("ID"));
            }
        }
        products.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我期望products.get(" id"((从ID列检索数据,而是我得到一个包含符号和gibberish的字符串。

有人对我如何实现这一目标有任何想法吗?

预先感谢!

在此要注意的两个要点

  1. BufferedReader是Java中的一类,它从字符输入流中读取文本,缓冲字符,以便有效阅读字符,行和数组。
  2. BufferedReader中的每一行是一个字符串输入流,您需要将字符串与分隔符分开,通常是a,'

尝试这样的希望,这就是您在这里要完成的工作。

try {
        URL url = new URL("URL here");
       URLConnection urlConn = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    ((URLConnection) urlConn).getInputStream()));
      
         String row;
         while ((row = in.readLine()) != null) {
            String[] values = row.split(","); // separator
            System.out.println("Product ID= " +values[0]); // change 0 to the column index of the file 
            }
        
       in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

谢谢快乐编码< 3!

最新更新