Java 文件处理完全指南:创建、读取、写入和删除文件详细解析

Java 文件处理是日常编程中常见的任务之一,涉及到对文件的创建、读取、写入和删除等操作。Java 提供了多种类库来实现文件操作,尤其是 java.iojava.nio 包。下面是关于如何在 Java 中处理文件的详细指南。

1. 文件创建

在 Java 中创建文件有多种方式,可以使用 File 类(传统的 I/O 类)或者 Path 类(NIO 包)。

使用 File 类创建文件


import java.io.File;
import java.io.IOException;

public class FileCreationExample {
    public static void main(String[] args) {
        File file = new File("example.txt");
        try {
            if (file.createNewFile()) {
                System.out.println("文件创建成功: " + file.getName());
            } else {
                System.out.println("文件已经存在.");
            }
        } catch (IOException e) {
            System.out.println("发生错误: " + e.getMessage());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 使用 Files 类(NIO)创建文件


    import java.nio.file.*;
    
    public class FileCreationNIOExample {
        public static void main(String[] args) {
            Path path = Paths.get("example.txt");
            try {
                Files.createFile(path);
                System.out.println("文件创建成功.");
            } catch (IOException e) {
                System.out.println("发生错误: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 2. 文件读取

    读取文件通常使用 FileReader, BufferedReaderFiles 类来实现。

    使用 FileReader 读取文件


    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    
    public class FileReadExample {
        public static void main(String[] args) {
            try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                System.out.println("发生错误: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 使用 Files 类读取文件


    import java.nio.file.*;
    import java.io.IOException;
    import java.util.List;
    
    public class FileReadNIOExample {
        public static void main(String[] args) {
            Path path = Paths.get("example.txt");
            try {
                List<String> lines = Files.readAllLines(path);
                for (String line : lines) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                System.out.println("发生错误: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 3. 文件写入

    写入文件可以使用 FileWriter, BufferedWriter, 或 Files 类。若文件不存在,FileWriter 会创建文件。

    使用 FileWriter 写入文件


    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.IOException;
    
    public class FileWriteExample {
        public static void main(String[] args) {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) {
                writer.write("这是新写入的内容.\n");
                writer.write("第二行内容.");
            } catch (IOException e) {
                System.out.println("发生错误: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 使用 Files 类写入文件


    import java.nio.file.*;
    import java.io.IOException;
    
    public class FileWriteNIOExample {
        public static void main(String[] args) {
            Path path = Paths.get("example.txt");
            String content = "这是新写入的内容.\n第二行内容.";
            try {
                Files.write(path, content.getBytes());
                System.out.println("文件写入成功.");
            } catch (IOException e) {
                System.out.println("发生错误: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 4. 文件删除

    删除文件时,可以使用 File 类或 Files 类来实现。

    使用 File 类删除文件


    import java.io.File;
    
    public class FileDeleteExample {
        public static void main(String[] args) {
            File file = new File("example.txt");
            if (file.delete()) {
                System.out.println("文件已删除.");
            } else {
                System.out.println("文件删除失败.");
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 使用 Files 类删除文件


    import java.nio.file.*;
    
    public class FileDeleteNIOExample {
        public static void main(String[] args) {
            Path path = Paths.get("example.txt");
            try {
                Files.delete(path);
                System.out.println("文件已删除.");
            } catch (IOException e) {
                System.out.println("删除文件失败: " + e.getMessage());
            }
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 5. 其他常用的文件操作

  • 检查文件是否存在

  • File file = new File("example.txt");
    if (file.exists()) {
        System.out.println("文件存在.");
    } else {
        System.out.println("文件不存在.");
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 获取文件的路径

  • File file = new File("example.txt");
    System.out.println("文件的绝对路径: " + file.getAbsolutePath());
  • 1.
  • 2.
  • 获取文件的大小

  • File file = new File("example.txt");
    System.out.println("文件大小: " + file.length() + " 字节");
  • 1.
  • 2.
  • 6. 总结

  • 文件创建:可以使用 File.createNewFile()Files.createFile()
  • 文件读取:可以使用 FileReader + BufferedReaderFiles.readAllLines()
  • 文件写入:可以使用 FileWriter + BufferedWriterFiles.write()
  • 文件删除:可以使用 File.delete()Files.delete()
  • 作者:心上之秋

    物联沃分享整理
    物联沃-IOTWORD物联网 » Java 文件处理完全指南:创建、读取、写入和删除文件详细解析

    发表回复