2. Objectives
On completion of this period, you would be able
to learn
• File class
• Example program using file class
• Example program to check the directory properties
http://improvejava.blogspot.in/ 2
3. Recap
In the last class, we have studied about the concept of
streams
• Stream is a flow bits of information either to or
from input or output devices into or from a
program
• The various input output stream classes
http://improvejava.blogspot.in/ 3
4. File Class
• It directly deals with files and the file system
• File class does not specify how information is
retrieved from or stored in files
• It describes the properties of a file itself
http://improvejava.blogspot.in/ 4
5. File Class contd..
• File class defines many methods that obtain the
standard properties of a File object
• getName( ) returns the name of the file
• getParent( ) returns the name of the parent
directory
http://improvejava.blogspot.in/ 5
6. File Class contd..
• exists( ) returns true if the file exists, false if it does
not
• length() : returns length of a file in bytes
• isFile() : check whether the file is normal file or
directory file
http://improvejava.blogspot.in/ 6
7. // Program for finding File properties
The following program shows the use of File class
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s); }
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");
http://improvejava.blogspot.in/ 7
8. // Program for finding File properties
Contd . . .
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not" + " a
directory"));
p(f1.isFile() ? "is normal file" : "might be a named
pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified: " + f1.lastModified());
p("File size: " + f1.length() + " Bytes");
}
}
• When you run the program, it displays the
properties of the selected file
http://improvejava.blogspot.in/ 8
9. Creating Directories
• File utility methods are mkdir( ) and mkdirs( )
• The mkdir ( ) method creates a directory, returning
true on success and false on failure
• mkdirs( ) method creates a directory for which no
path exists
http://improvejava.blogspot.in/ 9
10. Other Method Related To Directory
• isDirectory (): returns Boolean can be used to
check the given file is a directory
true directory
false a file
• list() : returns the list of file names available in
given directory.
http://improvejava.blogspot.in/ 10
11. // Program for checking for directory properties
import java.io.File;
class DirList {
public static void main(String args[]) {
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println("Directory of " + dirname);
String s[] = f1.list();
http://improvejava.blogspot.in/ 11
12. // Program for checking for directory properties
Contd . . .
for (int i=0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");
} else {
System.out.println(s[i] + " is a file"); } }
} else {
System.out.println(dirname + " is not a directory");
}}}
• Running this program produces output based on
what was in “/java” directory
http://improvejava.blogspot.in/ 12
13. Summary
• File class describes the properties of a file itself
• File defines many methods that obtain the standard
properties of a File object
• File utility methods for creating directories are
mkdir( ) and mkdirs( )
http://improvejava.blogspot.in/ 13
14. Quiz
1. File class does not specify how information is
retrieved from or stored in files
A. True
B. False
http://improvejava.blogspot.in/ 14
15. Quiz contd..
2. Which File method returns the name of the file
A. getName()
B. getParent( )
C. exists()
http://improvejava.blogspot.in/ 15
16. Frequently Asked Questions
1. Write a java program to find the various
properties of a File class
2. Explain File class, methods and attributes of it
http://improvejava.blogspot.in/ 16
Notas del editor
When designing an applet, you must remember the things that an applet cannot do. Some Web browsers or applet viewers may place different restrictions on applets. In general, applets cannot: read files on a local system write files to the local system delete files on the local system rename files on the local system create a directory on the local system list directory contents check for the existence of a file obtain the type, size or modification time of a file create a network connection to a system other than the one it was loaded from define system properties invoke any program on the local system manipulate any threadgroup, other than its own access or load classes other than the standard ones in Java api define classes that are part of packages on the local system