hadoop怎么判斷文件是否存在

小億
115
2024-01-03 06:11:03

Hadoop提供了FileSystem類來(lái)操作文件系統(tǒng),可以使用該類的exists方法來(lái)判斷文件是否存在。以下是一個(gè)示例代碼:

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 {
        // 創(chuàng)建Hadoop配置對(duì)象
        Configuration conf = new Configuration();
        
        // 創(chuàng)建文件系統(tǒng)對(duì)象
        FileSystem fs = FileSystem.get(conf);
        
        // 要判斷的文件路徑
        Path filePath = new Path("/path/to/file");
        
        // 判斷文件是否存在
        boolean fileExists = fs.exists(filePath);
        
        // 輸出判斷結(jié)果
        if (fileExists) {
            System.out.println("文件存在");
        } else {
            System.out.println("文件不存在");
        }
        
        // 關(guān)閉文件系統(tǒng)對(duì)象
        fs.close();
    }
}

請(qǐng)注意,上述代碼中的/path/to/file是一個(gè)示例文件路徑,您需要根據(jù)實(shí)際情況將其替換為您要判斷的文件的實(shí)際路徑。

0