Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you doing this for logging purposes? If so there are <a href="http://en.wikipedia.org/wiki/Java_logging_framework" rel="noreferrer">several libraries for this</a>. Two of the most popular are <a href="http://logging.apache.org/log4j/" rel="noreferrer">Log4j</a> and <a href="http://logback.qos.ch/" rel="noreferrer">Logback</a>.</p> <h2>Java 7+</h2> <p>If you just need to do this one time, the <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html" rel="noreferrer">Files class</a> makes this easy:</p> <pre><code>try { Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND); }catch (IOException e) { //exception handling left as an exercise for the reader } </code></pre> <p><sup><strong>Careful</strong>: The above approach will throw a <code>NoSuchFileException</code> if the file does not already exist. It also does not append a newline automatically (which you often want when appending to a text file). <a href="https://stackoverflow.com/a/44301865/18511">Steve Chambers's answer</a> covers how you could do this with <code>Files</code> class.</sup></p> <p>However, if you will be writing to the same file many times, the above has to open and close the file on the disk many times, which is a slow operation. In this case, a buffered writer is better:</p> <pre><code>try(FileWriter fw = new FileWriter("myfile.txt", true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println("the text"); //more code out.println("more text"); //more code } catch (IOException e) { //exception handling left as an exercise for the reader } </code></pre> <p><strong>Notes:</strong></p> <ul> <li>The second parameter to the <code>FileWriter</code> constructor will tell it to append to the file, rather than writing a new file. (If the file does not exist, it will be created.)</li> <li>Using a <code>BufferedWriter</code> is recommended for an expensive writer (such as <code>FileWriter</code>).</li> <li>Using a <code>PrintWriter</code> gives you access to <code>println</code> syntax that you're probably used to from <code>System.out</code>.</li> <li>But the <code>BufferedWriter</code> and <code>PrintWriter</code> wrappers are not strictly necessary.</li> </ul> <hr> <h2>Older Java</h2> <pre><code>try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true))); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } </code></pre> <hr> <h2>Exception Handling</h2> <p>If you need robust exception handling for older Java, it gets very verbose:</p> <pre><code>FileWriter fw = null; BufferedWriter bw = null; PrintWriter out = null; try { fw = new FileWriter("myfile.txt", true); bw = new BufferedWriter(fw); out = new PrintWriter(bw); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } finally { try { if(out != null) out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } try { if(bw != null) bw.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } try { if(fw != null) fw.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } } </code></pre>
    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. 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.
    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