Home  >  Article  >  Java  >  Java Example - Append data to file

Java Example - Append data to file

黄舟
黄舟Original
2017-02-15 10:10:231664browse

The following example demonstrates using the filewriter method to append data to a file:

/*
 author by w3cschool.cc
 Main.java
 */import java.io.*;public class Main {
   public static void main(String[] args) throws Exception {
      try {
         BufferedWriter out = new BufferedWriter
         (new FileWriter("filename"));
         out.write("aString1\n");
         out.close();
         out = new BufferedWriter(new FileWriter
         ("filename",true));
         out.write("aString2");
         out.close();
         BufferedReader in = new BufferedReader
         (new FileReader("filename"));
         String str;
         while ((str = in.readLine()) != null) {
            System.out.println(str);
         }
      }
      in.close();
      catch (IOException e) {
         System.out.println("exception occoured"+ e);
      }
   }}

The output result of running the above code is:

aString1
aString2

above It is a Java example - the content of appending data to a file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn