|
|
The java.io Package - File Class
- used to access file and directory objects using the file-naming, path conventions of the implementing operating system
- the class has three constructors
File(String pathname)
File(String parent, String child)
File(File parent, String child)
where,
parent is the pathname
child is the filename
- used to create an instance of a File BUT does not actually create a file
// does not create a file on the system
new File("test.txt");
- however, you can use the createNewFile() method
File f = new File("test.txt");
// returns 'false' if file exists
f.createNewFile();
- or, the method createTempFile() which creates the file in the default temporary directory using specified file extensions
- the class has four CONSTANTS which define properties of the file conventions on the operating system
- char separatorChar
- the field is initialized to hold the system separator
/ for UNIX
\ for Win32
: for Mac
- String separator
- a string representation of the separatorChar
- char pathSeparator
- initialized to hold the character used by the system to separate file names in a list
: for UNIX
; for Win32
- String pathSeparator
- string representation of the pathSeparator character
FileName Methods
- there are a number of methods for retreiving filenames, paths, etc
getAbsolutePath() getAbsoluteFile()
getCanonicalPath() getCanonicalFile()
getName()
getParent() getParentFile()
getPath()
compareTo()
toURL()
- the absolute path is system dependent and may include relative indicators
For example, the following code creates a file
'test2.txt' in the directory directly above the
current directory:
File f1 = new File("..", "test2.txt")
f1.createNewFile();
System.out.println( f1.getAbsolutePath() );
Output (on Win98):
D:\Java\jeg\io\..\test2.txt
- the canonical path is the same as the absolute path BUT all relative indicators are resolved
For example,
System.out.println( f1.getCanonicalPath() );
Output (on Win98):
// '..' in absolute path is resolved
D:\Java\jeg\test2.txt
- toURL() will construct a valid URL identifier for the File
System.out.println( f.toURL() );
Output:
file:/D:/Java/jeg/io/test1.txt
| Note |
- the File class overrides the Object.equals() method.
Two files are equal() if they have the same path, NOT if they refer to the same underlying file system object.
|
File Status Methods
Modifiying Files and Directories
- there are a number of methods for modifiying files and creating directories
delete() mkdir() listFiles()
deleteOnExit() mkdirs() listRoots()
renameTo()
- list() and listFiles() can be used with FilenameFilters ie '*'
- listRoots() returns the system drives
- while renameTo() will change the name of the file on the system, the reference will return the original path and name
// File object reference
File f = new File("test.txt");
f.createNewFile(); // creates the file
// new File reference
File f2 = new File("testRename.txt");
f.renameTo(f2); // renames the file
System.out.println( f.getAbsolutePath() );
Output (on Win98):
D:\Java\jeg\io\test1.txt // original path for 'f'
And if you check to see which file actually
exists on the system:
System.out.println( f.exists() );
System.out.println( f2.exists() );
Output:
false
true
| Note |
- There is no method which allows you to change directories!
|
Security
- many of the above methods will work correctly only if they are allowed by the security permissions
- for example, an Applet would probably not be allowed to create a new file
Source Code for Examples
|