java.io package contain all the required
classes and interface for the input and output process. This java.io package is
provides to process the input and produce the output, by using this java.io
package we can perform the File input and output operations.
File, FileReader, BufferedReader, Filewriter,
BufferedWriter and PrintWriter are very common classes for the File I/O
operations.
Here I provided the frequent asking questions
from the File I/O operations topic.
We can create file by using createNewFile()
method and mkdir() method is to create directory.
File f=new File("file_name.txt");
f.createNewFile();
File f=new File("directory_name");
f.mkdir();
2)
What are the FileReader and BufferedReader?
We have 2 ways to read data from the file.
FileReader:-
We can use FileReader to read data from the file. FileReader
contain read() and read(char[ ] ch) methods to read the data char by char from
the file.
BufferedReader:-
BufferedReader read the file line by line and it can’t
communicate directly with the file, it can communicate with some reader object.
It is most enhance reader.
BufferedReader contain the read(), read(char[] ch), close() and readLine()
methods
3)
What are the FileWriter, BufferedWriter and PrintWriter?
We have 3 ways to write character data into a file.
1. FileWriter:-
Note: - if the
specified file is not already available then this constructor will create that
file.
Methods:-
write(int ch):- to write single character.
write(char[] ch):- to write characters.
write(String s):- to write string.
flush():- to guaranty that total data should be written
properly to the file including last character.
close():- to close the string.
delete():- to delete file or directory.
2. BufferedWriter:-
To overcome FileWriter problem we use BufferedWriter to write
text data in to the file.
In this we have one extra method “newline()” to insert a newline.
BufferedWriter can’t communicate directly with the file.
In this approach we should take newline() method for every line
so length of the code will be increased.
3. PrintWriter:-
To overcome the BufferedWriter Problem we use PrintWriter to
write any type of data into the file.
printWriter is most enhance writer. If file not available then I
will create the file and write data into file.
Methods:-
Here we have some more extra methods print(char ch), print(int
i), print(double d), print(String s),
print(Boolean b).
4)
What are the most enhanced Writer and Reader?
The most enhanced writer is PrintWriter and the most reader is
BufferedReader.
5)
What is the purpose of flush() method?
This method is to guaranty that total data should be written
properly to the file including last character.
6)
How can we list out the files and directories in a specific directory?
The files and directories are list out from a
specific directory by using list() method.
import java.io.*;
class FileDemo
{
public static void main(String[] args) throws IOException
{
int count=0;
File f=new File("D:\\Java");
String[] s=f.list();
for(String s1:s)
{
count++;
System.out.println(s1);
}
System.out.println(count);
}
}
7)
What is the difference between write(100) and print(100) of PrintWriter class?
In the case of write(100) the corresponding character ‘d’ will
be written in to the file where as print(100) is written int value ‘100’.
8)
What is the difference between Reader Writer and input output streams?
Generally we use Reader and Writer to handle data where as we
can use input and output streams to handle binary data(like images, videos
files, audio files ect.).
We use FileOutPutStream to write binary data to the file where
as we use FileInputStream to read binary data from the file.
9) What is the serialization?
Serialization is interface in java.io package,
serialization is to convert an object from java supported form to file support
form or network supported form.
FileOutputStream
and ObjectOutputStream classes are implementing Serialization.
10) What is the Deserialization?
Deserialization is interface in java.io package,
Deserialization is to convert an object from file support form or network
supported form to java supported form.
FileInputStream
and ObjectInputStream classes are implementing Deserialization.
11) What is the Externalization?
In
Serialization, JVM is perform the all the activities in this case programmer
doesn’t have any control. In this process all the objects are save in the file,
which is creates the performance problem.
Externalization is overcome these problem, by using
Externalization all the activities perform by programmer.
12) What is the use of SerialVersionUID?
When
serialization sender side JVM will generate a unique id based on the .class
file and at the time deserialization receiver side JVM will compare that unique
id with local class unique id, if both ID’s match then deserialization will
perform, that unique id is SerialVersionUID.
No comments:
Post a Comment