Interview Questions on Java - File Handlings
1. How to read a File?
By using FileReader ex:-
FileReader in = new FileReader("file1.txt);
in.read(x);// reads all elemetnts of file1.txt into the Array x.
2. How to write Contents into a file.
By using FileWriter
ex:-
FileWriter out = new FileWriter("file1.txt");
out.write("abc");
try
{
out.flush();
out.close();
}
catch(FileNotFoundException ex)
{
//some code
}
3. Can we write the elements line by line by using FileWriter?
No.But you can use BufferedWriter.In BufferedReader you can insert a new line by using newLine();
ex:- out.newLine();
4. Can we read the elements line by line by using FileReader?
No.But you can use BufferedReader.In BufferedReader you can read entire line by using readLine();
ex:- in.readLine();
5. What is Serialization?
Serialization is the process of converting an object into a stream of bytes.
6. What is marker Interface?
Interface which has no methods.
7. What is serializable ?
It is a marker Interface which is used to achieve srialization. Without implementing serializable marker interface we can not do serialization.
8. What is de-serialization?
De-serialization is the process of constructing an object from a stream of bytes.
9. How many ways can we create the objects?
1. By using new() operator.
2. By calling newInstance() method
ex: Class c1 = class.forName(classname);
Object o1 = c1.newInstance();
3. By using Clone() method.
4. while de-serialization
FileInputStream fis = new FileInputStream(file1);
Object o1= in.readObject();
10. What is Externelization?
It is an Interface that extends serializable marker Interface.It is used for attribute wise operation.
11. What is transient variable ?
transient is a key word used for variables to avoid serialization.