![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.IOException; import java.util.Scanner; import org.apache.hadoop.fs.*; public class G_CreateAndDelAlterVersion { public static void CreateFile(FSDataOutputStream out,FileSystem fs,Path p_remotepath) { try{ out = fs.create(p_remotepath); System.out.println("create successfully"); } catch(Exception e) { e.printStackTrace(); } } public static void DelFile(FileSystem fs,Path p_remotepath) { try{ if(fs.delete(p_remotepath, true)) { System.out.println("del successfully"); } } catch(Exception e) { e.printStackTrace(); } } public static void main(String arg[]) { try{ Var_init var = new Var_init(); if(!var.fs.exists(var.p_remoteFilePath)) CreateFile(var.out,var.fs,var.p_remoteFilePath); else { RemoteIterator<LocatedFileStatus> remoteIterator = var.fs.listFiles(var.p_remoteFilePath, true); if(remoteIterator.hasNext()) { System.out.println("File is not empty , are u sure u want to delete it? y/n"); Scanner sc = new Scanner(System.in); String str = sc.next(); if(str.equals("y")) { DelFile(var.fs,var.p_remoteFilePath); } else if(str.equals("n")) { System.out.println("Cancle"); } else { System.out.println("plz input right common"); } } } } catch(Exception e){ e.printStackTrace(); } } }
Var_init类参考 https://www.cnblogs.com/MiraculousB/p/13848744.html