0% found this document useful (0 votes)
33 views

Java Bytearrayoutputstream

The FileWriter class inherits from OutputStreamWriter and is used for writing streams of characters to files. It has several constructors that allow creating FileWriter objects associated with File objects or file descriptors. Once created, FileWriter objects have methods like write() to write characters, strings, or portions of arrays to files. The example demonstrates creating a FileWriter, writing sample text to a file, then reading it back with a FileReader.

Uploaded by

Malu Sakthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java Bytearrayoutputstream

The FileWriter class inherits from OutputStreamWriter and is used for writing streams of characters to files. It has several constructors that allow creating FileWriter objects associated with File objects or file descriptors. Once created, FileWriter objects have methods like write() to write characters, strings, or portions of arrays to files. The example demonstrates creating a FileWriter, writing sample text to a file, then reading it back with a FileReader.

Uploaded by

Malu Sakthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JAVA - FILEWRITER CLASS

http://www.tuto rialspo int.co m/java/java_file write r_class.htm Co pyrig ht © tuto rials po int.co m

T his class inherits from the OutputStreamWriter class. T he class is used for writing streams of characters.

T his class has several constructors to create required objects.

Following syntax creates a FileWriter object g iven a File object.

FileWriter(File file)

Following syntax creates a FileWriter object g iven a File object.

FileWriter(File file, boolean append)

Following syntax creates a FileWriter object associated with a file descriptor.

FileWriter(FileDescriptor fd)

Following syntax creates a FileWriter object g iven a file name.

FileWriter(String fileName)

Following syntax creates a FileWriter object g iven a file name with a boolean indicating whether or not to append
the data written.

FileWriter(String fileName, boolean append)

Once you have FileWriter object in hand, then there is a list of helper methods, which can be used manipulate the
files.

SN Methods with Desc ription

1 public void write(int c ) throws IO Exc eption


Writes a sing le character.

2 public void write(c har [] c , int offset, int len)


Writes a portion of an array of characters starting from offset and with a leng th of len.

3 public void write(String s, int offset, int len)


Write a portion of a String starting from offset and with a leng th of len.

Example:
Following is the example to demonstrate class:

import java.io.*;

public class FileRead{

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

File file = new File("Hello1.txt");


// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();

//Creates a FileReader Object


FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); //prints the characters one by one
fr.close();
}
}

T his would produce the following result:

This
is
an
example

You might also like