|Java IO Introduction|
|Java IO Overview|
|Java Byte + Char Arrays|
|Java System.in / .out / .error|
|Java Readers / Writers|
|Java Input Parsing|
|Java Concurrent IO|
|Java IO Exception Handling|
File class in the Java IO API gives you access to the underlying file system. Using the
File class you can:
This text will tell you more about how.
File only gives you access to the file and file system meta data.
If you need to read or write the content of files, you should do so using either FileInputStream,
FileOutputStream or RandomAccessFile.
Before you can do anything with the file system or
File class, you must obtain a
instance. Here is how that is done:
File file = new File("c:\\data\\input-file.txt");
Simple, right? The
File class also has a few other constructors you can use to instantiate
File instances in different ways.
Once you have instantiated a
File object you can check if the corresponding file actually
exists already. The
File class constructor will not fail if the file does not already exists.
You might want to create it now, right?
To check if the file exists, call the
exists() method. Here is a simple example:
File file = new File("c:\\data\\input-file.txt"); boolean fileExists = file.exists();
To read the length of a file in bytes, call the
length() method. Here is a simple
File file = new File("c:\\data\\input-file.txt"); long length = file.length();
To rename (or move) a file, call the method
renameTo() on the
File class. Here is
a simple example:
File file = new File("c:\\data\\input-file.txt"); boolean success = file.renameTo(new File("c:\\data\\new-file.txt"));
As briefly mentioned earlier, the
renameTo() method can also be used to move a file to a different
directory. The new file name passed to the
renameTo() method does not have to be in the same directory
as the file was already residing in.
renameTo() method returns
boolean (true or false), indicating whether the renaming
was successful. Renaming of moving a file may fail for various reasons, like the file being open, wrong file
To delete a file call the
delete() method. Here is a simple example:
File file = new File("c:\\data\\input-file.txt"); boolean success = file.delete();
delete() method returns
boolean (true or false), indicating whether the deletion
was successful. Deleting a file may fail for various reasons, like the file being open, wrong file
File object can point to both a file or a directory.
You can check if a
File object points to a file or directory, by calling its
method. This method returns
true if the
File points to a directory, and
File points to a file. Here is a simple example:
File file = new File("c:\\data"); boolean isDirectory = file.isDirectory();
You can obtain a list of all the files in a directory by calling either the
listFiles() method. The
list() method returns an array of String's with
the file and / or directory names of directory the
File object points to. The
returns an array of
File objects representing the files and / or directories in the directory
File points to.
Here is a simple example:
File file = new File("c:\\data"); String fileNames = file.list(); File files = file.listFiles();