Professional Documents
Culture Documents
IO Serialization
• All streams behave in the same manner, even if the actual physical
devices to which they are linked differ. Thus, the same I/O classes and
methods can be applied to any type of device.
12
java.io.File
+File(pathname: String) Creates a File object for the specified pathname. The pathname may be a
directory or a file.
+File(parent: String, child: String) Creates a File object for the child under the directory parent. child may be a
filename or a subdirectory.
+File(parent: File, child: String) Creates a File object for the child under the directory parent. parent is a File
object. In the preceding constructor, the parent is a string.
+exists(): boolean Returns true if the file or the directory represented by the File object exists.
+canRead(): boolean Returns true if the file represented by the File object exists and can be read.
+canWrite(): boolean Returns true if the file represented by the File object exists and can be written.
+isDirectory(): boolean Returns true if the File object represents a directory.
+isFile(): boolean Returns true if the File object represents a file.
+isAbsolute(): boolean Returns true if the File object is created using an absolute path name.
+isHidden(): boolean Returns true if the file represented in the File object is hidden. The exact
Obtaining file definition of hidden is system-dependent. On Windows, you can mark a file
properties and hidden in the File Properties dialog box. On Unix systems, a file is hidden if
its name begins with a period character '.'.
manipulating file +getAbsolutePath(): String Returns the complete absolute file or directory name represented by the File
object.
+getCanonicalPath(): String Returns the same as getAbsolutePath() except that it removes redundant
names, such as "." and "..", from the pathname, resolves symbolic links (on
Unix platforms), and converts drive letters to standard uppercase (on Win32
platforms).
+getName(): String Returns the last name of the complete directory and file name represented by
the File object. For example, new File("c:\\book\\test.dat").getName() returns
test.dat.
+getPath(): String Returns the complete directory and file name represented by the File object.
For example, new File("c:\\book\\test.dat").getPath() returns c:\book\test.dat.
+getParent(): String Returns the complete parent directory of the current directory or the file
represented by the File object. For example, new
File("c:\\book\\test.dat").getParent() returns c:\book.
+lastModified(): long Returns the time that the file was last modified.
+delete(): boolean Deletes this file. The method returns true if the deletion succeeds.
13
+renameTo(dest: File): boolean Renames this file. The method returns true if the operation succeeds.
Example:
import java.io.*;
class FileDemo {
public static void main(String args[]){
File f1 = new File("a.txt");
System.out.println("File Name: " + f1.getName());
System.out.println("Path: " + f1.getPath());
System.out.println("Is Absolute : " + f1.isAbsolute());
System.out.println("Absolute Path: " + f1.getAbsolutePath());
System.out.println("Parent: " + f1.getParent());
System.out.println(f1.exists() ? "exists" : "does not exist");
System.out.println("Is a Directory: "+ f1.isDirectory());
System.out.println("Is a File: "+ f1.isFile());
System.out.println("Can Write? "+ f1.canWrite());
System.out.println("Can Read? "+ f1.canRead());
System.out.println("File last modified: " + f1.lastModified());
System.out.println("File size: " + f1.length() + " Bytes");
}
}
Wednesday, March 25, 2020 14
Scanner class
The java.util.Scanner class was used to read strings and primitive values
from the console:
Scanner s= new Scanner(System.in);
To read from a file, create a Scanner for a file, as follows:
Scanner input = new Scanner(new File(filename));
Example:
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("b.txt");
Scanner input = new Scanner(file);
while(input.hasNext()) {
String s = input.nextLine();
System.out.println(s);
}
input.close();
}
}
PrintWriter class
Thejava.io.PrintWriter class can be used to create a file and write data
to a text file.
PrintWriter output = new PrintWriter(filename);
Then, you can invoke the print, println methods on PrintWriter object.
Example:
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) throws Exception {
File f = new File("b.txt");
PrintWriter output = new PrintWriter(f);
output.print("Java class ");
output.println(40);
output.print("LPU ");
output.println(85.5);
output.print('a');
output.close();
}
}
Reading and Writing Files
In Java, all files are byte-oriented
FileInputStream and FileOutputStream create byte streams linked
to files
To open a file, create an object of one of these classes, specifying the
name of the file as an argument to the constructor