Demonstrate FileWriter. : File Writer « File Input Output « Java






Demonstrate FileWriter.

   
import java.io.FileWriter;
import java.io.IOException;

class FileWriterDemo {
  public static void main(String args[]) throws IOException {
    String source = "test";
    char buffer[] = new char[source.length()];
    source.getChars(0, source.length(), buffer, 0);

    FileWriter f0 = new FileWriter("file1.txt");
    for (int i = 0; i < buffer.length; i += 2) {
      f0.write(buffer[i]);
    }
    f0.close();

    FileWriter f1 = new FileWriter("file2.txt");
    f1.write(buffer);
    f1.close();

    FileWriter f2 = new FileWriter("file3.txt");

    f2.write(buffer, buffer.length - buffer.length / 4, buffer.length / 4);
    f2.close();
  }
}

   
    
  








Related examples in the same category

1.Use FileWriter to write an array of strings to a file.
2.Append or add some contents to the file
3.Using a Reader and a Writer, returns a String from an InputStream
4.Implementation of a fast Writer.