Monday, June 1, 2015

File Writer

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


//This class inherits from the OutputStreamWriter class. The class is used for writing
//streams of characters

//FileWriter(File file)
//FileWriter(File file, boolean append)
//FileWriter(FileDescriptor fd)
//FileWriter(String fileName)
//FileWriter(String fileName, boolean append)

// Methods with Description

// public void write(int c) throws IOException
// Writes a single character.

// public void write(char [] c, int offset, int len)
// Writes a portion of an array of characters starting from offset and with a length of len.

// public void write(String s, int offset, int len)
// Write a portion of a String starting from offset and with a length of len.

public class FileWriterClassDemo {

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

File file = new File("test.txt");
file.createNewFile();

FileWriter fWriter = new FileWriter(file);
fWriter.write("Space to write awesomeness");
fWriter.flush();
fWriter.close();

FileReader fReader = new FileReader(file);

char[] a = new char[50];
fReader.read(a);

for (char c : a) {
System.out.print(c);
}
fReader.close();
}
}


No comments:

Post a Comment