Reputation: 2424
I am using this java program to read a csv file contents from a specified URL in my program, the problem with it is that it does not fetch last 64 records, i.e it gets all the records into my specified myHello.csv file except last 64.Plz help.
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");
}
}
Upvotes: 0
Views: 1857
Reputation: 53694
You are "double" consuming from the BufferedReader in each loop. You call readLine()
twice for each loop, but only write it once (so you are dropping every other line).
Upvotes: 1