[英]Reading CSV file from url in Java
我正在尝试从给定的URL中读取CSV文件并进行打印。我找不到错误的地方,但是它什么也没打印出来。 有人可以帮我找到需要解决的内容,以便该程序正常运行吗? 谢谢。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws IOException {
URL url = new URL("http://gist.githubusercontent.com/yonbergman/7a0b05d6420dada16b92885780567e60/raw/114aa2ffb1c680174f9757431e672b5df53237eb/data.csv");
URLConnection connection = url.openConnection();
InputStreamReader input = new InputStreamReader(connection.getInputStream());
BufferedReader buffer = null;
String line = "";
String csvSplitBy = ",";
try {
buffer = new BufferedReader(input);
while ((line = buffer.readLine()) != null) {
String[] room = line.split(csvSplitBy);
System.out.println(line);
System.out.println("room [capacity =" + room[0] + " , price=" + room[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这太晚了,只是碰巧找到了这篇文章,尽管它对用户不起作用,但我们从中得到了一些提示,并希望发布对我们有用的内容:
//...
//Java IO File imports
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
//Java SQL imports
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// ...
// Button code
public void doBrowserReadFromTwitterPHP() {
// ...
// Entering try catch
try {
// ...
// Connect to DB2 to access Twitter table(s)
Connection connection = DB2TWConnector.getConnection();
// ...
// make available CSV in URL
URL urlCSV = new URL(
"http://www.yourweburl.com/AppDevFolder/resttwitterpubpostcsv.php");
// ...
// establish connection to file in URL
URLConnection urlConn = urlCSV.openConnection();
// ...
InputStreamReader inputCSV = new InputStreamReader(
((URLConnection) urlConn).getInputStream());
// ...
BufferedReader br = new BufferedReader(inputCSV);
// ...
// Declare String to hold file to Split from URL
String line;
String RoleNameVal = RoleNameValue.toString();
String UserNameVal = UserNameValue.toString();
String PageIDVal = PageID.toString();
// ...
// Read file accordingly
while ((line = br.readLine()) != null) {
// ...
// Split file based on Delimiter in question "MyStrToMyDotoboseSectoid"
String[] values = line.split(" MyStrToMyDotoboseSectoid "); // separator
// ...
// Declare and plug values obtained from Split
String strPostID = values[0];
String strPostName = values[1];
String strPostMessage = values[2];
String strPostDate = values[3];
String strPostURL = values[4];
String strPostStamp = values[5];
// ...
// Plug in App generated Info
String strRoleID = RoleNameVal.trim();
String strUserName = UserNameVal.trim();
String strPageID = PageIDVal.trim();
//DEBUG Purposes, comment out in prod
System.out.println("strPostID = " +strPostID);
System.out.println("strPostName = " +strPostName);
System.out.println("strPostMessage = " +strPostMessage);
System.out.println("strPostDate = " +strPostDate);
System.out.println("strPostURL = " +strPostURL);
System.out.println("strPostStamp = " +strPostStamp);
System.out.println("strRoleID = " +strRoleID);
System.out.println("strUserName = " +strUserName);
System.out.println("strPageID = " +strPageID);
// ...
System.out.println("Entering DB2 query...");
// ...
PreparedStatement prep = connection
.prepareStatement("insert into DB2ADMIN.TWITTER_WEB_POST values(?,?,?,?,?,?,?,?,?)");
// ... ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
System.out.println("Loading values values to Columns...");
// ...
// make avail expected value
prep.setString(1, strPostID);
prep.setString(2, strPostDate);
prep.setString(3, strPostName);
prep.setString(4, strPostURL);
prep.setString(5, strPostMessage);
prep.setString(6, strPostStamp);
prep.setString(7, strRoleID);
prep.setString(8, strUserName);
prep.setString(9, strPageID);
connection.setAutoCommit(false);
prep.execute();
connection.setAutoCommit(true);
// ...
System.out.println("DB2 Twitter values added...");
}
// clean stuff up
br.close();
connection.close();
} catch (Exception e) {
SQLException e2 = ((SQLException) e).getNextException();
String more = "";
if (e2 != null)
more = " : " + e2.getMessage();
try {
throw new SQLException("Connecting to DB, using: "
+ UserNameValue + " account: " + e.getMessage() + more);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.