• android 创建、删除 文件和文件夹 测试


    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            File sdCard = Environment.getExternalStorageDirectory();    //取得sdCard 的目录
            
            try {
                //测试创建文件和文件夹
                boolean CreateFileResult = createFile(sdCard.getAbsolutePath(),"sdCreateFile.sq",false); //创建文件
                boolean CreateDirResult = createFile(sdCard.getAbsolutePath(),"sdCreateDir.dir",true);    //创建文件夹
                
            } catch (IOException e) {e.printStackTrace();}
            //测试删除文件和文件夹
            boolean deleteFileResult = deleteFile(sdCard.getAbsolutePath(), "sdCreateFile.sq");
            boolean deleteDirResult  = deleteFile(sdCard.getAbsolutePath(), "sdCreateDir.dir");
            
        }
        
        //创建文件夹或目录
        boolean createFile(String path,String fileName,boolean isDir) throws IOException
        {
            File dir = new File(path);
            if(dir.exists()!=true) return false;        //path no exists
            File newFile = new File(path+"/"+fileName);
            return isDir==true? newFile.mkdir():newFile.createNewFile();
        }
        
        //删除文件夹或目录
        boolean deleteFile(String path,String fileName)
        {
            File dir = new File(path);
            if(dir.exists()!=true)return false;        //path no exists
            return new File(path+"/"+fileName).delete();
        }
  • 相关阅读:
    NYOJ 205
    NYOJ 187
    NYOJ 105
    NUOJ 88
    NYOJ 70
    LL(1)算法
    MATLAB的一些基础知识
    Ubuntu raid5+lvm实验
    空间滤波
    认识weblogic的各个机构
  • 原文地址:https://www.cnblogs.com/laoquans/p/3068341.html
Copyright © 2020-2023  润新知