Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Read all text from a file</h2> <p>Here's a compact, robust idiom for Java 7, wrapped up in a utility method:</p> <pre><code>static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); } </code></pre> <h2>Read lines of text from a file</h2> <p>Java 7 added a <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29" rel="noreferrer">convenience method to read a file as lines of text,</a> represented as a <code>List&lt;String&gt;</code>. This approach is "lossy" because the line separators are stripped from the end of each line.</p> <pre><code>List&lt;String&gt; lines = Files.readAllLines(Paths.get(path), encoding); </code></pre> <p>In Java 8, <code>BufferedReader</code> added a new method, <a href="https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#lines--" rel="noreferrer"><code>lines()</code></a> to produce a <code>Stream&lt;String&gt;</code>. If an <code>IOException</code> is encountered while reading the file, it is wrapped in an <a href="https://docs.oracle.com/javase/8/docs/api/java/io/UncheckedIOException.html" rel="noreferrer"><code>UncheckedIOException</code></a>, since <code>Stream</code> doesn't accept lambdas that throw checked exceptions.</p> <pre><code>try (BufferedReader r = Files.newBufferedReader(path, encoding)) { r.lines().forEach(System.out::println); } </code></pre> <p>There is also a <code>Files.lines()</code> method which does something very similar, returning the <code>Stream&lt;String&gt;</code> directly. But I don't like it. The <code>Stream</code> needs a <code>close()</code> call; this is poorly documented on the API, and I suspect many people don't even notice <code>Stream</code> has a <code>close()</code> method. So your code would look very similar, like this:</p> <pre><code>try (Stream&lt;String&gt; lines = Files.lines(path, encoding)) { lines.forEach(System.out::println); } </code></pre> <p>The difference is that you have a <code>Stream</code> assigned to a variable, and I try avoid that as a practice so that I don't accidentally try to invoke the stream twice.</p> <h2>Memory utilization</h2> <p>The first method, that preserves line breaks, can temporarily require memory several times the size of the file, because for a short time the raw file contents (a byte array), and the decoded characters (each of which is 16 bits even if encoded as 8 bits in the file) reside in memory at once. It is safest to apply to files that you know to be small relative to the available memory.</p> <p>The second method, reading lines, is usually more memory efficient, because the input byte buffer for decoding doesn't need to contain the entire file. However, it's still not suitable for files that are very large relative to available memory.</p> <p>For reading large files, you need a different design for your program, one that reads a chunk of text from a stream, processes it, and then moves on to the next, reusing the same fixed-sized memory block. Here, "large" depends on the computer specs. Nowadays, this threshold might be many gigabytes of RAM. The third method, using a <code>Stream&lt;String&gt;</code> is one way to do this, if your input "records" happen to be individual lines. (Using the <code>readLine()</code> method of <code>BufferedReader</code> is the procedural equivalent to this approach.)</p> <h2>Character encoding</h2> <p>One thing that is missing from the sample in the original post is the character encoding. There are some special cases where the platform default is what you want, but they are rare, and you should be able justify your choice.</p> <p>The <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html" rel="noreferrer"><code>StandardCharsets</code></a> class define some constants for the encodings required of all Java runtimes:</p> <pre><code>String content = readFile("test.txt", StandardCharsets.UTF_8); </code></pre> <p>The platform default is available from <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html#defaultCharset%28%29" rel="noreferrer">the <code>Charset</code> class</a> itself:</p> <pre><code>String content = readFile("test.txt", Charset.defaultCharset()); </code></pre> <hr> <p>Note: This answer largely replaces my Java 6 version. The utility of Java 7 safely simplifies the code, and the old answer, which used a mapped byte buffer, prevented the file that was read from being deleted until the mapped buffer was garbage collected. You can view the old version via the "edited" link on this answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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