I am reading data from API response in batches of bytes which is of Content-Type = text/CSV and using Java's NIO package to transfer bytes between two Channels. I want to write API responses in batches to the same File (APPEND). With below code append doesn't seem to work correctly, it's more of overriding results.
And once all the data is written then I also want to print the number of total lines in CSV.
private void downloadFile_NIO(String encoded_token) throws Exception { long start_Range = 0; long end_Range = 5000; long batch_size = 5000; long totalsize = 1080612174; long counter = 0; //Path path = Paths.get(FILE_NAME); //long lines = 0; while (start_Range <= totalsize) { URL url = new URL(FILE_URL); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Bearer " + encoded_token ); connection.setDoOutput(true); connection.setRequestMethod("GET"); ReadableByteChannel readableByteChannel = Channels.newChannel(connection.getInputStream()); FileOutputStream fileOutputStream=new FileOutputStream(new File(FILE_NAME),true); fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); // lines = Files.lines(path).count(); // System.out.println("lines->" + lines); System.out.println(); fileOutputStream.close(); readableByteChannel.close(); counter = counter + 1; if (counter < 2) { start_Range = start_Range + batch_size + 1; end_Range = end_Range + batch_size; } else { start_Range = start_Range + batch_size; end_Range = end_Range + batch_size; } } }