提供个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录 文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删 除该目录

提供个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录 文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删 除该目录

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();
        }
    }

}
View Code

Var_init类参考 https://www.cnblogs.com/MiraculousB/p/13848744.html