1. import org.apache.hadoop.conf.Configuration;
    2. import org.apache.hadoop.fs.FileSystem;
    3. import org.apache.hadoop.fs.Path;
    4. public class isFIleExist {
    5. public static void main(String[] args) {
    6. try {
    7. String filename = "test";
    8. //经典配置四件套
    9. Configuration conf = new Configuration();
    10. conf.set("fs.defaultFS","hdfs://localhost:9000");
    11. conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
    12. FileSystem fs = FileSystem.get(conf);
    13. if(fs.exists(new Path(filename))){
    14. System.out.println("文件或目录存在");
    15. }else{
    16. System.out.println("文件或目录不存在");
    17. }
    18. fs.close();
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }