Java Reference
In-Depth Information
We now present the most important classes for reading and writing ASCII text files
and some of their methods.
10.1.1
The class File
The class File is the Java abstraction of a file or directory on the hard disk or
some other storage medium; we shall use the term 'hard disk' in the following.
Below we list a constructor and some methods we need:
File(String filename)
boolean exists()
boolean delete();
boolean renameTo(File newName)
boolean createNewFile()
boolean isDirectory()
String getName()
String getPath()
!
File(String filename) creates a file object for a file with name filename . Note
that neither an existing file with that name is opened nor is a file created on
the hard disk.
exists() returns true if the file exists.
delete() deletes the file from the hard disk and returns true if successful or
false otherwise, e.g. if the file did not exist.
renameTo(File newName) renames the file. The new name is that of the file object
newName .
createNewFile() creates a file on the hard disk. The file name is that specified
in the constructor, but only if the file does not already exist. If successful it
returns true and false otherwise.
isDirectory() returns true if the file is a directory and false otherwise.
getName() returns just the file name, without the path but with extension.
getPath() returns the whole path including file name and extension.
Possible file names (the last one is directory) are, for example
testFile.txt
C:/Java/kurs/FileIO/scr/testFile.txt
C: \\ Java \\ kurs \\ FileIO \\ scr \\ testFile.txt
/Java/kurs/FileIO/testFile.txt
./FileIO/testFile.txt
C:/Java/
!
Note that under MS Windows one can use both / or \ as a separator. Because of
the special meaning of backslash in Java strings it has to be duplicated.
Search WWH ::




Custom Search