private void testFile(){ try { File[] files=File.listRoots();//路径:/ File data=Environment.getDataDirectory();//data根目录,路径:/data File downloadCache=Environment.getDownloadCacheDirectory();//路径:/cache boolean hasSD=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);//SD卡是否装载 File storage1=Environment.getExternalStorageDirectory();//获取SD卡存储根目录,//路径:/storage/emulated/0 // File storagePublic=Environment.getExternalStoragePublicDirectory(""); File root=Environment.getRootDirectory();//路径:/system String storageState=Environment.getExternalStorageState();//路径:mounted FuzeLog.d(TAG, "DataDirectory:" + data.getAbsolutePath() + ",DownloadCacheDirectory:" + downloadCache.getAbsolutePath() + ",ExternalStorageDirectory:" + storage + ",RootDirectory:" + root.getAbsolutePath() + ",ExternalStorageState:" + storageState); File filesDir=context.getFilesDir();//路径:/data/data/tv.fuzegame.fuzelauncher/files FuzeLog.d(TAG, "FilesDir:" + filesDir.getAbsolutePath()); String relativePath=filesDir.getPath();//相对路径 String absolutePath=filesDir.getAbsolutePath();//绝对路径 String fileName=filesDir.getName();//文件(夹)名 String parentPath=filesDir.getParent();//父目录 boolean isDirec=filesDir.isDirectory();//是否为文件夹true-是,false-否 boolean has=filesDir.exists();//文件(夹)是否存在true-是,false-否 FuzeLog.d(TAG, "相对路径:" + relativePath+",绝对路径:" + absolutePath+",文件(夹)名:" + fileName+",父目录:" + parentPath+",是否为文件夹:" + isDirec+",文件(夹)是否存在:"+has); createFile(filesDir); } catch (Exception e) { e.printStackTrace(); } } private void createFile(File file) { boolean is = file.isDirectory();//是否为文件夹true-是,false-否 if (is) { FuzeLog.d(TAG, "创建前:" + file.getAbsolutePath()); String path = file.getAbsolutePath(); String name = "ABC";//你要新建的文件夹名或者文件名 String pathx = path + name; // File filex=new File(path,name);//方法2,不需要在方法中拼路径 File filex = new File(pathx); boolean is2 = filex.exists();//判断文件(夹)是否存在 if (!is2) { filex.mkdir();//创建文件夹 //filex.createNewFile();//创建文件 } FuzeLog.d(TAG, "创建后,重命名前:" + filex.getAbsolutePath()); //重命名 String parentPath=filex.getParent(); File filexR=new File(parentPath,"abb");//File filex=new File(parentPaht+"abc") filex.renameTo(filexR); FuzeLog.d(TAG, "重命名后:" + filex.getAbsolutePath()); //删除文件(夹) file.delete();//立即删除 //file.deleteOnExit();//程序退出后删除,只有正常退出才会删除 } }
android 路径及文件操作
最新推荐文章于 2021-08-24 18:08:54 发布