FileUtil
直接上代码看Log
private void getTest() {
File tt;
String ttt;
tt=Environment.getRootDirectory();
Log.e("android根目录:",tt.toString());
tt=Environment.getDataDirectory();
Log.e("android数据目录:",tt.toString());
tt=Environment.getDownloadCacheDirectory();
Log.e("android下载缓存目录:",tt.toString());
tt=Environment.getExternalStorageDirectory();
Log.e("android SD卡目录:",tt.toString());
ttt=Environment.getExternalStorageState();
Log.e("外部存储设备的当前状态",ttt);
}
E/android根目录:: /system
E/android数据目录:: /data
E/android下载缓存目录:: /cache
E/android SD卡目录:: /storage/emulated/0
E/外部存储设备的当前状态: mounted
再来看看常用的方法
/**
* 创建文件
*/
public File createFile(String fileName) throws IOException {
String SDPATH = Environment.getExternalStorageDirectory() + "/";
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 创建目录
*/
public File createDir(String fileName) throws IOException {
String SDPATH = Environment.getExternalStorageDirectory() + "/";
File dir = new File(SDPATH + fileName);
dir.mkdir();
return dir;
}
/**
* 修改文件夹和文件名
*/
public File renameFile(File file, File newFile) {
file.renameTo(newFile);
return file;
}
/**
* 删除文件或文件夹
*/
public void deleteFile(File file) {
file.delete();
}
/**
* 获取文件或文件夹的相对路径
*/
public String getFilePath(File file) {
String path = file.getPath();
return path;
}
/**
* 获取文件或文件夹的绝对路径
*/
public String getFileAbsolutePath(File file) {
String path = file.getAbsolutePath();
return path;
}
/**
* 获取文件或文件夹的名称
*/
public String getFileName(File file) {
String fileName = file.getName();
return fileName;
}
/**
* 获取文件或文件夹的父目录
*/
public String getFileParentPath(File file) {
String parentPath = file.getParent();
return parentPath;
}
/**
* 是否是文件
*/
public boolean hasFile(File file) {
return file.isFile();
}
/**
* 是否是文件夹
*/
public boolean hasDirectory(File file) {
return file.isDirectory();
}
/**
* 列出文件夹下的所有文件和文件夹名
*/
public File[] getAllFile(File file) {
return file.listFiles();
}
总结,File根本不需要工具类,直接file.就可了,用工具类是画蛇添足。
上面工具类知识为了记录有哪些常用方法
================补充================
root.getCanonicalPath()是获取标准路径

浙公网安备 33010602011771号