从指定 URL 读取的 .csv 文件

.csv file reading from a specified URL

我正在使用这个 java 程序从指定的 URL 读取 .csv 文件。该程序的唯一问题是它不会将 csv 文件中的最后 64 条记录读取到 myHello.csv 文件中。这是程序。

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-.com/ccx/service/customreport2/-ProjectsFE/YIN.05-get?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);
                bf.newLine();
            }
        }
        catch (Exception e){

            System.out.print("Exception");
        }
        System.out.println("done");

    }
}

while 循环中,您读取了不需要的行,因此在重新计算 while 语句时跳过了行。

正如@Ultimus 提到的,您读取缓冲区两次,只写入一次。还要确保关闭 reader 和编写器,这样它们将强制刷新并且一切都将完成。