Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're not interested in the character based data the text file is containing, just stream it "raw" as bytes.</p> <pre><code>InputStream input = new BufferedInputStream(new FileInputStream("C:/logs.txt")); byte[] buffer = new byte[8192]; try { for (int length = 0; (length = input.read(buffer)) != -1;) { System.out.write(buffer, 0, length); } } finally { input.close(); } </code></pre> <p>This saves the cost of unnecessarily massaging between bytes and characters and also scanning and splitting on newlines and appending them once again.</p> <p>As to the performance, you may find <a href="http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly" rel="nofollow">this article</a> interesting. According the article, a <a href="http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html" rel="nofollow"><code>FileChannel</code></a> with a 256K byte array which is read through a wrapped <a href="http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html" rel="nofollow"><code>ByteBuffer</code></a> and written directly from the byte array is the fastest way.</p> <pre><code>FileInputStream input = new FileInputStream("C:/logs.txt"); FileChannel channel = input.getChannel(); byte[] buffer = new byte[256 * 1024]; ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); try { for (int length = 0; (length = channel.read(byteBuffer)) != -1;) { System.out.write(buffer, 0, length); byteBuffer.clear(); } } finally { input.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