1 import java.text.SimpleDateFormat; 2 import java.util.Scanner; 3 4 import org.apache.hadoop.fs.FileStatus; 5 import org.apache.hadoop.fs.FileSystem; 6 import org.apache.hadoop.fs.LocatedFileStatus; 7 import org.apache.hadoop.fs.Path; 8 import org.apache.hadoop.fs.RemoteIterator; 9 10 11 public class I_delFile { 12 public static void ReadFilesStatus(FileStatus fileinfo) 13 { 14 SimpleDateFormat format = new SimpleDateFormat( 15 "yyyy-MM-dd HH:mm:ss"); 16 System.out.println("file path :"+fileinfo.getPath()); 17 } 18 public static void DelFile(FileSystem fs,Path p_remotepath) 19 { 20 try{ 21 if(fs.delete(p_remotepath, true)) 22 { 23 System.out.println("del successfully"); 24 } 25 else 26 { 27 System.out.println("del fail"); 28 } 29 } 30 catch(Exception e) 31 { 32 e.printStackTrace(); 33 } 34 } 35 public static void main(String arg[]) 36 { 37 try{ 38 Var_init var = new Var_init(); 39 String s_path = "/user"; 40 Path p_path = new Path(s_path); 41 RemoteIterator<LocatedFileStatus> remoteIterator = var.fs.listFiles(p_path, true); 42 while(remoteIterator.hasNext()) 43 { 44 var.fileinfo = remoteIterator.next(); 45 ReadFilesStatus(var.fileinfo); 46 } 47 System.out.println("plz input the file u want to del like /user/hadoop/test"); 48 Scanner sc = new Scanner(System.in); 49 String path = sc.next(); 50 Path remotepath = new Path(path); 51 DelFile(var.fs,remotepath); 52 } 53 catch(Exception e){ 54 e.printStackTrace(); 55 } 56 } 57 }
Var_init类参考 https://www.cnblogs.com/MiraculousB/p/13848744.html