Home > Java > javaTutorial > body text

Java Example - Append data to file

黄舟
Release: 2017-02-15 10:10:23
Original
1668 people have browsed it

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);
      }
   }}
Copy after login

The output result of running the above code is:

aString1
aString2
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template