Monday 14 November 2016

Working with FileWriter



 FileWriterclass creates a Writer that can be used to write to a file.
 Its most commonly used constructors are:
FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj,boolean append)
It can throw a FileNotFoundException.
Here, filePath is the full path name of the file, and fileObj is a File object that describes the file. If append is true, then output is appended to the end of the file.
 Creation of FileWriter is not dependent on the File already existing. FileWriterwill create the file before opening it for output.
 In case of opening a read only file, an IOException will be thrown.
 Example:
import java.io.*;
classFilewriter
{
public static void main(String args[]) throws IOException
 {
  String s="Hello this information is written to file using filewriter\nIt is used for writting";
FileWriter f=new FileWriter("File1.txt");
char b[]=new char[s.length()];
FileWriter f1=new FileWriter("File2.txt");
f1.write(b);
System.out.println("Data Successfully Written");
 }
}
Output:
Data Successfully Written

No comments:
Write comments