【问题标题】:Reading CSV file from url in Java从Java中的url读取CSV文件
【发布时间】:2017-11-10 08:56:25
【问题描述】:

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


    【解决方案1】:

    你的网址有问题。

    Github 在请求标头中使用特定策略来防止获取您的数据内容。

    查看here如何从github获取内容

    【讨论】:

    • 对不起,但仍然不起作用......它不会从 while 循环中打印任何内容
    • 我调试了你写的代码,由于某种原因“inputLine”为空,所以它没有进入循环。你有什么理由吗? ://
    【解决方案2】:

    来晚了,碰巧发现了这个帖子,虽然它对用户不起作用,但我们从中得到了一些指示,并想发布对我们有用的东西:

    //...
       //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();
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      相关资源
      最近更新 更多