Friday, 2 October 2015

Streams

Important classes in streams
1) File
2) FileWriter
3) FileReader
4)BufferedWriter
5)BufferedReader
6)PrintWriter

File f=new File("newfile.txt");

The above line checks if there exists any physical file already available with name newfile.txt. 
If file already exists, then f points to that file
If it does not exist, it will create a Java File Object to represent the name newfile.txt .(The above statement doesn't create physical file)

various methods in File class are:
exists(), createNewFile(), mkdir(),isFile(), isDirectory(),delete() -> all these methods returns Boolean output
String[] list() -> To retrieve list of files in a directory.

FileWriter
Use FileWriter Object to write Character Data(text data) to the file.
Constructors (Creates a new physical file. If file already exists it will override)
FileWriter fw=new FileWriter(String name);
FileWriter fw=new FileWriter(File f);

Constructors (Following constructors append to the existing physical file)
FileWriter fw=new FileWriter(String name, boolean append);
FileWriter fw=new FileWriter(File f, boolean append);

Methods of File Writer class:
write(int ch) -> to write single character to the file.
write(char[] ch) -> to write array of characters to the file
write(String s) -> to write a String to the File
flush() -> To give the guarantee that, total data including last character is written properly to the file.
close() ->

Limitation: Insert line separator (\n), which is varied from system to system is very difficult

FileReader -> Its a low level reader to read text/char data from file
Use FileReader Object to read Character Data(text data) from the file.
Constructors 
FileReader fr=new FileReader(String name);
FileReader fr=new FileReader(File f);

Methods:
int read();  int read(char[])
   Reads next character returns its Unicode value. Perform type casting while printing.
   Returns -1, on end of file

Limitation: Reading character by character is not convenient and impacts performance.

BufferedWriter -To write Character Data(text data) to the file. It can't communicate directly with file. It can communicate via some writer object only. When a BufferedWriter is closed, automatically underlying file writer will be closed.

Constructors (Creates a new physical file. If file already exists it will override)
BufferedWriter fw=new BufferedWriter(Writer w);
BufferedWriter fw=new BufferedWriter(Writer w, int buffersize);


Methods of Buffered Writer class:

write(int ch) -> to write single character to the file.
write(char[] ch) -> to write array of characters to the file
write(String s) -> to write a String to the File
newLine() -> To insert a line separator 
flush() -> To give the guarantee that, total data including last character is written properly to the file.
close() ->

BufferedReader -> It is a most enhanced reader to read character data from file, either line by line or character by charecter. It can't communicate directly with file. It can communicate via some reader object only. When a BufferedReader is closed, automatically underlying file reader will be closed.

Use BufferedReader Object to read Character Data(text data) from the file.
Constructors 
BufferedReader fr=new BufferedReader(Reader r);
BufferedReader fr=new BufferedReader(Reader r, int buffersize);

Methods:
int read();  int read(char[]), void close(), String readLine()
   Reads next character returns its Unicode value. Perform type casting while printing.
   Returns -1, on end of file
readLine() returns NULL on end of file.

PrintWriter -Most enhanced writer to write Character Data(text data) to the file. It can write any Primitive type data directly to the file.   It can communicate directly with file as well as via some writer object

Constructors (Creates a new physical file. If file already exists it will override)
PrintWriter pw=new PrintWriter(String filename);
PrintWriter pw =new PrintWriter(File f);
PrintWriter pw=new PrintWriter(Writer w);


Methods of Buffered Writer class:

print(char ch) -> to write single character to the file.
print(int i)
print(double d)
print(boolean b)
print(String s)

println(int i)  -> prints and then inserts a new line.

write(100)  -> corresponding character will be added to file
print(100) -> int value 100 will be added to file

POINTS TO NOTE:
1. Most enhanced writer to write character data to the file is PrintWriter, where as the most enhanced reader to read character data from the file is BufferedReader.
2. One should use Readers and Writers to handle Character data(Text data). Use Streams to handle Binary Data.
   We can use FileInputStream to read Binary data from file and we can use FileOutputStream to write Binary data to the File (like Images, Video files, Audio Files etc).


No comments:

Post a Comment