Yarden Halperin
Yarden Halperin

Reputation: 21

Reading CSV file from url in Java

I'm trying to read a CSV file from a given URL and print in. I can't find where I was wrong but it doesn't print me anything. Can someone please help me to find what I need to fix so this program will work? Thanks.

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

    }

}

Upvotes: 0

Views: 11520

Answers (2)

Dököll
Dököll

Reputation: 11

this is late, just happen to find this post, while it did not work for user, we got some pointers from it, and wanted to post what worked for us:

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

Upvotes: 1

Mike Adamenko
Mike Adamenko

Reputation: 3002

The problem with your url.

Github uses specific policies in the request header to prevent getting content of your data.

See here how to get content from github

Upvotes: 0

Related Questions