Introduction to Java I/O and the File Class with Practical Example
This article introduces Java I/O fundamentals, explains the four I/O interface groups, details the File class methods, provides a complete code demo with execution results, and highlights important points such as mkdirs behavior and cross‑platform path separators.
I/O problems are one of the main bottlenecks in modern web applications. This series introduces the basic architecture of Java I/O libraries, the mechanisms of disk I/O, network I/O, and the working mode of NIO.
Since Java 1.4, NIO has been introduced to improve I/O performance. The I/O classes reside in the java.io package and can be divided into four groups:
Byte‑oriented I/O interfaces: InputStream and OutputStream
Character‑oriented I/O interfaces: Reader and Writer
Disk‑oriented I/O interface: File
Network‑oriented I/O interface: Socket (not in java.io but discussed together)
The first two groups mainly concern data formats, while the latter two concern data transmission methods. The core I/O issue is either the data format affecting operations or the transmission method affecting operations, ultimately answering the question of what data to write where.
File Class
The following section introduces common methods of the File class and some important concepts:
A File object represents a file or directory on disk.
File provides platform‑independent methods to operate on files or directories.
The class works directly with the file system.
File does not specify how information is read from or written to a file.
package io;
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) throws IOException {
File file = new File("f:/migu");
file.mkdir();
// Check if the directory exists
if (file.exists() && file.isDirectory()) {
System.out.println("migu 目录存在");
File file1 = new File("f:/migu/UES.txt");
File file2 = new File("f:\\migu\\CMU.txt");
// Create files
file1.createNewFile();
file2.createNewFile();
File file3 = new File("f:/migu/插件/支付中心");
// Create directory and any missing parent directories
file3.mkdirs();
File[] files = file.listFiles();
// List files in the directory
for (File f : files) {
System.out.println("migu目录下的文件名:" + f.getName());
System.out.println("migu目录文件的绝对路径:" + f.getAbsolutePath());
}
} else {
System.out.println("migu 目录不存在");
}
}
}Execution result:
migu 目录存在
migu目录下的文件名:CMU.txt
migu目录文件的绝对路径:f:\migu\CMU.txt
migu目录下的文件名:UES.txt
migu目录文件的绝对路径:f:\migu\UES.txt
migu目录下的文件名:插件
migu目录文件的绝对路径:f:\migu\插件Two important points to note:
mkdirs creates the directory represented by the abstract pathname and also creates any missing parent directories.
In lines 16‑17 the code uses two different path separators. Windows uses "\\" (escaped as "\\\\" in Java) while Linux uses "/". Using "/" works on all operating systems, so it is recommended to use forward slashes for portability.
Original article: http://www.cnblogs.com/dongguacai/p/5656471.html
If you encounter problems while learning Java or want more resources, feel free to join the Java study group QQ: 495273252 .
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.