- Java IO Tutorial
- Java IO Overview
- Java IO: Files
- Java IO: Pipes
- Java IO: Networking
- Java IO: Byte & Char Arrays
- Java IO: System.in, System.out, and System.error
- Java IO: Streams
- Java IO: Input Parsing
- Java IO: Readers and Writers
- Java IO: Concurrent IO
- Java IO: Exception Handling
- Java IO: InputStream
- Java IO: OutputStream
- Java IO: FileInputStream
- Java IO: FileOutputStream
- Java IO: RandomAccessFile
- Java IO: File
- Java IO: PipedInputStream
- Java IO: PipedOutputStream
- Java IO: ByteArrayInputStream
- Java IO: ByteArrayOutputStream
- Java IO: FilterInputStream
- Java IO: FilterOutputStream
- Java IO: BufferedInputStream
- Java IO: BufferedOutputStream
- Java IO: PushbackInputStream
- Java IO: SequenceInputStream
- Java IO: DataInputStream
- Java IO: DataOutputStream
- Java IO: PrintStream
- Java IO: ObjectInputStream
- Java IO: ObjectOutputStream
- Java IO: Serializable
- Java IO: Reader
- Java IO: Writer
- Java IO: InputStreamReader
- Java IO: OutputStreamWriter
- Java IO: FileReader
- Java IO: FileWriter
- Java IO: PipedReader
- Java IO: PipedWriter
- Java IO: CharArrayReader
- Java IO: CharArrayWriter
- Java IO: BufferedReader
- Java IO: BufferedWriter
- Java IO: FilterReader
- Java IO: FilterWriter
- Java IO: PushbackReader
- Java IO: LineNumberReader
- Java IO: StreamTokenizer
- Java IO: PrintWriter
- Java IO: StringReader
- Java IO: StringWriter
Java IO: File
File class in the Java IO API gives you access to the underlying file system. Using the
File class you can:
- Check if a file or directory exists.
- Create a directory if it does not exist.
- Read the length of a file.
- Rename or move a file.
- Delete a file.
- Check if path is file or directory.
- Read list of files in a directory.
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.
Note: If you are using Java NIO you will have to use the java.nio.FileChannel class instead (you can use both, but in case you want a pure Java NIO solution).
Instantiating a java.io.File
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.
Check if File Exists
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();
Create a Directory if it Does Not Exist
You can use the
File class to create directories if they don't already exists. The
class contains the method
mkdirs() for that purpose.
mkdir() method creates a single directory if it does not already exist. Here is an example:
File file = new File("c:\\users\\jakobjenkov\\newdir"); boolean dirCreated = file.mkdir();
Provided that the directory
c:\users\jakobjenkov already exists, the above code will create a subdirectory
true if the directory was
false if not.
mkdirs() will create all directories that are missing in the path the
represents. Here is an example:
File file = new File("c:\\users\\jakobjenkov\\newdir"); boolean dirCreated = file.mkdirs();
Provided that the C drive exists, this example will create all the directories in the path
mkdirs() method will return
true if all the directories were created, and
false if not.
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();
Rename or Move File
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
Check if Path is File or Directory
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();
Read List of Files in Directory
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();