Java 文件 I/O


Java 文件 I/O

import java.io.*;

public class DemoJava {

   public static void main(String []args) throws IOException {

      File file = new File("/tmp/java.txt");

      // Create a File
      file.createNewFile();

      //  Creates a FileWriter Object using file object
      FileWriter writer = new FileWriter(file);

      // Writes the content to the file
      writer.write("This is testing for Java write...\n");
      writer.write("This is second line...\n");

      // Flush the memory and close the file
      writer.flush();
      writer.close();

      // Creates a FileReader Object
      FileReader reader = new FileReader(file);
      char [] a = new char[100];

      // Read file content in the array
      reader.read(a);
      System.out.println( a );

      // Close the file
      reader.close();
   }
}