How does Hadoop determine if a file exists?

Hadoop provides the FileSystem class for operating on the file system, and you can use the exists method of this class to determine if a file exists. Here is an example code:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FileExistsExample {
    public static void main(String[] args) throws Exception {
        // 创建Hadoop配置对象
        Configuration conf = new Configuration();
        
        // 创建文件系统对象
        FileSystem fs = FileSystem.get(conf);
        
        // 要判断的文件路径
        Path filePath = new Path("/path/to/file");
        
        // 判断文件是否存在
        boolean fileExists = fs.exists(filePath);
        
        // 输出判断结果
        if (fileExists) {
            System.out.println("文件存在");
        } else {
            System.out.println("文件不存在");
        }
        
        // 关闭文件系统对象
        fs.close();
    }
}

Please note that the /path/to/file in the above code is an example file path, you need to replace it with the actual path of the file you want to check.

bannerAds