使用java程序读写HDFS文件,判断文件是否存在

  1. 判断文件是否存在
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Chapter3 {
	public static void main(String[] args) {
		try{
     		string fileName = "test";
     					
     		Configuration conf = new Configuratio();
     		conf.set("fs.defaultFS","hdfs://localhost:9000");
          	conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
          			
          	FileSystem fs = FileSystem.get(conf);
          	if (fs.exsits(new Path(FileName))){
          		System.out.println("文件存在");
          	}else{
          		System.out.println("文件不存在");
		   }
	      fs.close();
   }
  catch(Exception e){
		e.printStackTrace();
   }
 }
}        		

你可能感兴趣的:(Hadoop常用命令,判断文件是否存在)