从Java中的url读取CSV文件

时间:2017-06-08 14:57:01

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

    }

}

2 个答案:

答案 0 :(得分:1)

这已经很晚了,碰巧发现这篇文章,虽然它对用户不起作用,我们从中得到了一些指示,并希望发布对我们有用的内容:

def solution(A):
    holes = [0]*(1+len(A))    # holes[0] isn't used
    for i in A:
        # if i is larger than N then it's not a permutation
        # if i appeared before in A, then the hole[i] should be 1, in such case it's not a permutation as well
        if i>len(A) or holes[i]==1:
            return 0
        holes[i] = 1
    return 1

答案 1 :(得分:0)

您的网址存在问题。

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

请参阅here如何从github获取内容