[英]How do you read contents of a CSV file downloaded into the browser?
我需要使用Java读取托管CSV文件的内容。 在托管CSV的网址上点击此文件,即可将文件下载到浏览器中。 如何访问此文件并读取其内容,而无需在本地进行任何操作?
目前我有:
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列检索数据,而不是得到包含符号和乱码的字符串。
有人对我如何实现这一目标有任何想法吗?
提前致谢!
这里要注意的两个要点
像这样希望,这就是您要在这里完成的。
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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.