Professional Documents
Culture Documents
IO Streams
IO Streams
By
Arvind Kumar
Asst. Professor
• An absolute file name (or full name) contains a file name with
its complete path and drive letter.
• For example, c:\book\Welcome.java
• File class does not contain the methods for reading and writing
file contents.
Methods and Constructors
Constructor:
File(String path_name)
File(String parent, String Child)
File(File parent, String Child)
• Creates a File object for the specified path name. The path name may be a
directory or a file.
Methods:
boolean isFile()
boolean isDirectory()
boolean exists()
boolean canRead()
boolean canWrite()
Methods and Constructors
String getName()
String getPath()
String getAbsolutePath()
long lastModified()
long length()
boolean delete()
boolean renameTo(File name)
boolean isAbsolute()
boolean isHidden()
File [] listFiles()
boolean createNewFile()
boolean mkdir()
boolean mkdirs()
Let’s Do It
• Java Program that reads a file name from the user, then
displays information about whether the file exists, whether the
file is readable, whether the file is writable, the type of file and
the length of the file in bytes.
Serializable
– Only an object that implements the Serializable interface can be
saved and restored by the serialization facilities.
try {
MyClass object2;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println("object2: " + object2);
}
catch(Exception e) {
System.out.println("Exception during deserialization: " + e);
System.exit(0);
}
}
}
transient Keyword
• Java transient keyword is used in serialization. If you define any data member
as transient, it will not be serialized.