Java File IO Tutorials
- Details
- Written by Nam Ha Minh
- Last Updated on 09 November 2024   |   Print Email
Reading and writing files are fundamental tasks in software development. The Java Development Kit (JDK) offers a comprehensive set of APIs that simplify working with files, directories, and various Input/Output operations, making it easier for programmers to handle these tasks efficiently. The tutorials below will guide you through Java’s File Input/Output operations step-by-step, helping you master these essential skills with ease:
1. Read Input from the User:
Learn to read user-entered input from keyboard in command line (console) with InputStreamReader, Console and Scanner classes:
- 3 ways for reading user's input from console in Java
- Java Console Input Output Examples
- Java Scanner Tutorial and Code Examples
2. Read and Write Files in Java:
The following articles show you how to read and write binary files with InputStream and OutputStream; and how to read and write text files with FileReader and FileWriter classes:
- How to Read and Write Binary Files in Java
- How to Read and Write Text File in Java
- How to read text file line by line in Java
3. File and Directory Listing:
Find the Java code examples to list files and directories (including nested files and directories) in the following posts. You will also learn how to get hard disk's used and free space, and calculate the number of files and directories in any directory.
- How to list files and directories in a directory
- List files and directories recursively in a directory
- List all drives with type, total space and free space in Java
- Calculate total files, sub directories and size of a directory
4. File and Directory Operations:
Learn how to perform common operations on files and directories, such as copy, move, rename, and delete - using both legacy File IO APIs and New IO APIs.
- Clean and remove a non-empty directory
- How to copy a directory programmatically in Java
- Copy File or Directory with Java NIO
- How to rename/move file or directory in Java
- Java IO - Common File and Directory Operations Examples
- How to use RandomAccessFile (java.io)
- How to use random access file with FileChannel (Java NIO)
5. Java InputStream and OutputStream Examples:
The following articles help you get in-depth understanding about different implementations of InputStream and OutputStream so you'll understand exactly when a FileReader, DataInputStream or ObjectInputStream can be used.
- Java DataInputStream and DataOutputStream Examples
- Java FileInputStream and FileOutputStream Examples
- Java FileReader and FileWriter Examples
- Java ObjectInputStream and ObjectOutputStream Examples
6. Java Serialization:
Read the following articles to understand the concepts and usage of serialization in Java IO. I'll also explain externalization which is an advanced form of serialization.
- Why Do We Need Serialization in Java?
- Java Serialization Basic Example
- Understand Externalization in Java
7. How to handle ZIP File with Java:
Using the Java IO APIs, programmers can easily write programs that work with compressed files in ZIP format. You can find in the following articles various code examples for reading (extracting) and writing (compressing) ZIP files:
- How to read contents of a ZIP file in Java
- How to compress files into ZIP format with Java
- How to extract a zip file in Java programmatically
- How to zip directories programmatically using Java
8. Miscellaneous Java File IO Tutorials:
Learn how to format string in the standard output (console), read/write configuration file, execute operating system's commands and watch file change events:
- Java String Format Examples
- Read and write configuration data using Preferences class
- How to Execute Operating System Commands in Java
- File change notification example with Watch Service API