java - Java中从url读取CSV文件

标签 java csv url inputstream

我正在尝试从给定的 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/

相关文章:

swift - 每当用户移动到其他页面时如何从 webview 获取 url

java - 分组 Subject 和教师并提高代码性能

java - 如何在 jenkins 插件中包含针对 JDK 1.8 的依赖项?

r - 当列名包含空格和特殊字符时从 data.table 包中读取?

Javascript 使用变量通过按钮创建 url

url - 让 Spring 告诉 FreeMarker 要使用的 URL 编码

java - 在 Java 中转换 ArrayList

java - 我的游戏没有登录 Google Play 游戏

c# - 将 .csv 转换为数据表时的额外引号

csv - 使用 VIM 从第 4 列之后的 csv 中删除所有数据