Common methods for file operations in Java

In Java, file operations are one of the common tasks in development. The following is a detailed explanation of some commonly used file manipulation methods:

1. Creation and writing of files

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

public class FileWriteExample {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            // 创建文件对象
            File file = new File("example.txt");

            // 创建文件写入流
            FileWriter writer = new FileWriter(file);

            // 写入内容
            writer.write("Hello, this is an example.");

            // 关闭流
            writer.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

2. Read content of the file

import java.io.BufferedReader;
import java.io.FileReader;
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);
            }

            // 关闭流
            reader.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

3. Inspection and operation of files and directories

import java.io.File;

public class FileCheckAndOperationExample {
    
    
    public static void main(String[] args) {
    
    
        // 创建文件对象
        File file = new File("example.txt");

        // 检查文件是否存在
        boolean exists = file.exists();
        System.out.println("File exists: " + exists);

        // 获取文件名
        String fileName = file.getName();
        System.out.println("File name: " + fileName);

        // 获取文件路径
        String filePath = file.getAbsolutePath();
        System.out.println("File path: " + filePath);

        // 创建目录
        File directory = new File("example_directory");
        boolean success = directory.mkdir();
        System.out.println("Directory created: " + success);

        // 列出目录内容
        File[] filesInDirectory = directory.listFiles();
        if (filesInDirectory != null) {
    
    
            for (File f : filesInDirectory) {
    
    
                System.out.println(f.getName());
            }
        }
    }
}

4. File copy and move

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class FileCopyAndMoveExample {
    
    
    public static void main(String[] args) {
    
    
        // 复制文件
        try {
    
    
            Path sourcePath = new File("source.txt").toPath();
            Path destinationPath = new File("destination.txt").toPath();
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

        // 移动文件
        try {
    
    
            Path sourcePath = new File("source.txt").toPath();
            Path destinationPath = new File("new_location/source.txt").toPath();
            Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

These are some basic examples of file operations in Java. Depending on your specific needs, you may need to use these methods in conjunction with the actual situation and pay attention to handling possible exceptions. In real projects, more advanced file manipulation libraries, such as Apache Commons IO or Guava, are often used to simplify the development process.

おすすめ

転載: blog.csdn.net/wykqh/article/details/135142996