gpt4 book ai didi

java - Java中从url读取CSV文件

转载 作者:行者123 更新时间:2023-12-02 12:56:04 26 4
gpt4 key购买 nike

我正在尝试从给定的 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();
}
}
}

关于java - Java中从url读取CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44439172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com