Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet69/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   65   66   67   68   69   70   71   72   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

183.
184.
When Line 3 is executed, the file is closed and the contents are
saved in the file.
Write a code sample that demonstrates how to delete a
file.
Answer:
The file class has a method called delete() that can be used to
delete a file. The following code demonstrates this:
File myNewFile = new File(“/usr/CoreJava.txt”);
boolean success = myNewFile.delete(); //Line 1
In the above code, when Line 1 is executed, the delete()
method deletes the specified file. It returns a boolean value that
indicates whether file deletion is successful or not.
Write a code sample that demonstrates how to rename a
file or directory.
Answer:
The file class has a method called renameTo() which can be used
to rename a file or a directory. The following code demonstrates
renaming a file:
File file1 = new File(“/usr/file1.txt”);
file1.createNewFile();
File file2 = new File(“/usr/file2.txt”);
file1.renameTo(file2); //Line 1
This code renames the file “file1.txt” to “file2.txt”. The following
code demonstrates renaming a directory:
File dir1 = new File(“/usr/dir1”);


185.
186.
a.
b.
c.
d.
e.
dir1.createNewFile();
File dir2 = new File(“/usr/dir2”);
dir1.renameTo(dir2); //Line 1
This code renames the directory dir1 to dir2
Which classes are used to serialize and de–serialize
objects?
Answer:
Serialization is the process of writing an object onto the file
system. The java.io.ObjectOutputStream can be used to
serialize objects. It has a writeObject() method which can be
used to achieve this. De–serialization is the process of restoring
a serialized object. The java.io.ObjectInputStream can be
used to serialize objects. It has a readObject() method which
can be used to achieve this.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling