Redesigned By Fadel Khudadeh
Reviewed By Waheed Al-Sayer
Advanced Java
Input & Output (I/O) Day 11
1
Input & Output (I/O) 2
JDK has two sets of IO packages:
the Standard IO (java.io) (since JDK 1.0)
the New IO (java.nio) (introduced in JDK 1.4).
In addition, JDK 1.5 introduced the formatted text-
IO via new classes Scanner/Formatter and printf().
Class java.io.File 3
The class java.io.File can represent either a file or a directory.
It also maintains two system-dependent properties, for you to write programs that are portable:
Directory Separator: Windows systems use backslash '\' (e.g., "c:\jdk\bin\java.exe"), while Unixes use forward slash '/' (e.g., "/usr/jdk/bin/java.exe"). This system-dependent property is maintained in the static field File.separator (as String) or File.separatorChar. (They failed to follow the naming convention for constants, which was adopted in JDK 1.2.)
Path Separator: Windows use semi-colon ';' to separate paths (or directories) white Unixes use colon ':'. This system-dependent value can be retrieved from static field File.pathSeparator (as String) or File.pathSeparatorChar.
The commonly used constructor in java.io.File is (not throwing FileNotFoundException?!):
public File(String fileOrDirName)
File file = new File("in.txt");
Common Methods 4
The commonly-used methods are:
For a directory, the following methods can be used
to list its contents:
public boolean exists() // tests if the file/directory exists.
public boolean isDirectory() // tests if this instance is a directory.
public boolean isFile() // tests if this instance is a file.
public boolean canRead() // tests if the file readable.
public boolean canWrite() // tests if the file writeable.
public long length() // returns the length of the file.
public boolean delete() // deletes the file or directory.
public void deleteOnExit() // deletes file or directory when program terminates.
public boolean renameTo(File dest) // renames the file
public boolean mkdir() // create this directory.
// List the contents of a directory
public String[] list()
public File[] listFiles() // List with a filename filter
public String[] list(FilenameFilter filter)
public File[] listFiles(FilenameFilter filter)
public File[] listFiles(FileFilter filter)
List directory content using list method 5
Example 1.1: list all files in c:\windows\system32
import java.io.File;
public class FileOne {
public static void main(String[] args) {
File dir = new File("c:\\windows\\system32");
if (dir.isDirectory()) {
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
}
}
}
6
Example 1.2: list exe files in c:\windows\system32
import java.io.File;
public class FileOne {
public static void main(String[] args) {
File dir = new File("c:\\windows\\system32");
if (dir.isDirectory()) {
String[] files = dir.list();
for (int i = 0; i < files.length; i++)
if(files[i].endsWith("exe"))
System.out.println(files[i]);
}
}
}
List exe files using list method
7
import java.io.File;
public class FileOne {
public static void main(String[] args) {
File dir = new File("c:\\windows\\system32");
if (dir.isDirectory()) {
String[] files = dir.list();
String ss[];
for (int i = 0; i < files.length; i++) {
ss=files[i].split("\\.");
if(ss.length >1 && ss[0].length() < 4)
System.out.println(files[i]);
}
}
}
}
Try it out ????
8
import java.io.File; // Example 1.3
public class FileOne {
public static void main(String[] args) {
File dir = new File("c:\\windows\\system32");
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
}
}
}
List Directory Using listFiles Method
9
import java.io.File; // Example 1.4
public class FileOne {
public static void main(String[] args) {
File dir = new File("c:\\windows\\system32");
if (dir.isDirectory()) {
File[] f = dir.listFiles();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i] +" - "+f[i].length());
}
}
}
}
List Directory Using listFiles Method
Student Time
Try it your self, write a code for the following methods:
10
// tests if the file/directory exists.
public boolean exists()
// tests if this instance is a directory.
public boolean isDirectory()
// tests if this instance is a file.
public boolean isFile()
// tests if the file readable.
public boolean canRead()
// tests if the file writeable.
public boolean canWrite()
Student Time
Try it your self, write a code for the following methods:
11
// returns the length of the file.
public long length()
// deletes the file or directory.
public boolean delete()
// deletes file or directory when program terminates.
public void deleteOnExit()
// renames the file
public boolean renameTo(File dest)
// create this directory.
public boolean mkdir()
12
public static void getRoots(){
File[] rts= File.listRoots();
System.out.println("Roots Report");
for (int i = 0; i < rts.length; i++) {
long free=rts[i].getFreeSpace()/(1024*1024); // in MB
long total=rts[i].getTotalSpace()/(1024*1024); // in MB
String canR=(rts[i].canRead())?"Yes":"No";
String canW=(rts[i].canWrite())?"Yes":"No";
System.out.println("Root: " + rts[i]);
System.out.println(" -Used["+(total-free)+" MB]");
System.out.println(" -Free["+ free +" MB]");
System.out.println(" -Total["+ total +" MB]");
System.out.println(" -Readable: " + canR);
System.out.println(" -Writable: " + canW);
}
}
13
Roots Report
Root: C:\
-Used[103820 MB]
-Free[849946 MB]
-Total[953766 MB]
-Readable: Yes
-Writable: Yes
Root: D:\
-Used[0 MB]
-Free[0 MB]
-Total[0 MB]
-Readable: No
-Writable: No
Root: E:\
-Used[289772 MB]
-Free[664094 MB]
-Total[953866 MB]
-Readable: Yes
-Writable: Yes
Root: F:\
-Used[311211 MB]
-Free[165725 MB]
-Total[476936 MB]
-Readable: Yes
-Writable: Yes
Root: G:\
-Used[0 MB]
-Free[0 MB]
-Total[0 MB]
-Readable: No
-Writable: No
Root: H:\
-Used[28 MB]
-Free[71 MB]
-Total[99 MB]
-Readable: Yes
-Writable: Yes
Output
Recursively list directory content 14
Recursively list the contents of a directory (similar to Unix's "ls -r" command or Windows “dir /s”).
public static void listRecursive(File dir) {
if (dir.isDirectory()) {
File[] items = dir.listFiles();
for (File item : items) {
System.out.println(item.getAbsoluteFile());
if (item.isDirectory())
listRecursive(item);
}
}
}
Recursively list directory content 15
Recursively list the contents of a directory (similar to Unix's "ls -r" command or Windows “dir /s”).
import java.io.File;
public class ListDirectoryRecusive {
public static void listRecursive(File dir) {
………
}
public static void main(String[] args) {
File dir = new File("d:\\fadel\\@Java\\@CourseWork");
listRecursive(dir);
}
} ……
c:\windows\system32\xwizards.dll - 432640
c:\windows\system32\xwreg.dll - 101888
c:\windows\system32\xwtpdui.dll - 201216
……
Output