FileWriter
FileWriter creates a writer that you can use to write a file.
Use constructors:
- FileWriter(String filepath)
- It is create a new file and it get file name in string.
- FileWriter(String filepath, boolean path)
- It is create a new file and it get file name in file object.
For Example
/**
*
* @author pradeep
*/
import java.io.*;
class FileWriterDemo
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("abc.txt");
fw.write("Hello pradeep");
fw.close();
}
catch(Exception e){System.out.println(e);
}
System.out.println("success");
}
}
Result: Output of this program is
run:
success
BUILD SUCCESSFUL (total time: 0 seconds)
FileReader
It is a class which is responsible for class creates a reader that you can use read the contents of a file.
It does the following task:
- Connect to standard input console.
- Converts the bytes string coming from the source into char stream.
Use constructors:
- FileReader(String filepath)
- FileReader(File fileobj)
For Example
/**
*
* @author pradeep
*/
import java.io.*;
class FileReaderDemo
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("abc.txt");
int i;
while((i=fr.read())!=-1)
System.out.println("FileReader is:"+(char)i);
fr.close();
}
}
Result: Output of this program:
run:
FileReader is:H
FileReader is:e
FileReader is:l
FileReader is:l
FileReader is:o
FileReader is:
FileReader is:p
FileReader is:r
FileReader is:a
FileReader is:d
FileReader is:e
FileReader is:e
FileReader is:p
BUILD SUCCESSFUL (total time: 0 seconds)
No comments:
Post a Comment