从指定的URL读取CSV文件

时间:2015-01-16 18:16:41

标签: java

我正在使用这个java程序从我的程序中的指定URL读取csv文件内容,问题是它不能获取最后64条记录,即它将所有记录都记录到我指定的myHello中.csv文件,除了最后64.Plz帮助。

package com.aamir;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class TestGetCSV {


    public static void main(String[] args) {

        try {

                URL url12  = new URL("https://wd5-impl-services1.workday.com/ccx/service/customreport2//ISU-YINT061-ProjectsFE/YINT061.05-OpenText?format=csv" );

                URLConnection uc = url12.openConnection();

                String userpass = "username" + ":" + "password";
                String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());

                uc.setRequestProperty ("Authorization", basicAuth);
               /* HttpURLConnection conn = (HttpURLConnection)uc;
                conn.setInstanceFollowRedirects( false );
                */

                InputStreamReader inStream = new InputStreamReader(uc.getInputStream());
                BufferedReader buff= new BufferedReader(inStream);

                String content2 = null;

                BufferedWriter bf = new BufferedWriter(new FileWriter("D:/myHello.csv"));
                while ((content2 = buff.readLine()) != null) {

                    bf.write(content2);

                    content2 = buff.readLine();
                    bf.flush();
                }
                }
                catch (Exception e){
                    e.printStackTrace();
                    System.out.print("KHONG TAI DUOC DU LIEU");
                }
                System.out.println("done");

                }


}

1 个答案:

答案 0 :(得分:1)

你是" double"从每个循环中的BufferedReader消耗。你为每个循环调用readLine()两次,但只写一次(所以你要删除每一行)。