Note that there are some explanatory texts on larger screens.

plurals
  1. POFaster way of copying data in Java?
    text
    copied!<p>I have been given a task of copying data from a server. I am using <code>BufferedInputStream</code> and output stream to copy the data and I am doing it byte by byte. Even though it is running but It is taking ages to copy the data as some of them are in 100's MBs, so definitely it is not gonna work. Can anyone suggest me any alternate of Byte by Byte copy so that my code can copy file that are in few Hundred MBs. Buffer is 2048.</p> <p>Here is how my code look like:</p> <pre class="lang-java prettyprint-override"><code>static void copyFiles(SmbFile[] files, String parent) throws IOException { SmbFileInputStream input = null; FileOutputStream output = null; BufferedInputStream buf_input = null; try { for (SmbFile f : files) { System.out.println("Working on files :" + f.getName()); if (f.isDirectory()) { File folderToBeCreated = new File(parent+f.getName()); if (!folderToBeCreated.exists()) { folderToBeCreated.mkdir(); System.out.println("Folder name " + parent + f.getName() + "has been created"); } else { System.out.println("exists"); } copyFiles(f.listFiles(), parent + f.getName()); } else { input = (SmbFileInputStream) f.getInputStream(); buf_input = new BufferedInputStream(input, BUFFER); File t = new File(parent + f.getName()); if (!t.exists()) { t.createNewFile(); } output = new FileOutputStream(t); int c; int count; byte data[] = new byte[BUFFER]; while ((count = buf_input.read(data, 0, BUFFER)) != -1) { output.write(data, 0, count); } } } } catch (IOException e) { e.printStackTrace(); } finally { if (input != null) { input.close(); } if (output != null) { output.close(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload