File
权限
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
File文件的模式
Activity.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。
Activity.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Activity.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
Activity.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
Environment(访问环境的变量)
//判断SD卡是否可用 true boolean b= Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //SD卡根目录的绝对路径 /storage/emulated/0 File file= Environment.getExternalStorageDirectory(); //获取内部存储的绝对路径 /data String path= Environment.getDataDirectory().getAbsolutePath();
File(操作文件)
/*操作文件*/ //获取File对象 File myfile=new File(path); //是否是文件夹 myfile.isDirectory(); //是否是文件 myfile.isFile(); //文件是否存在 myfile.exists(); //创建新文件 try { myfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //删除文件 myfile.delete(); //如果当前file对象是文件夹,则可以获得文件夹内包含的所有文件 File[] ffs= myfile.listFiles(); //文件的长度 myfile.length(); //文件重命名 myfile.renameTo(new File("")); //创建空的文件夹 myfile.mkdir();
FileOutputStream与FileInputStream 文件IO流
/*将内容写入文件*/ private void saveFile(Context context,String FilePath,String content){ try { FileOutputStream fileOutputStream= openFileOutput(FilePath,MODE_PRIVATE); fileOutputStream.write(content.getBytes()); fileOutputStream.flush(); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/*从SD卡中读取文件内容*/ private String readSDFile(String FileName){ String res=""; try { File file=new File(FileName); if(file.exists()==true){ InputStreamReader inputStreamReader = null; inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String lineStr; while ((lineStr = bufferedReader.readLine()) != null) { res += lineStr; } inputStreamReader.close(); }else{ return null; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res; }
操作File的常用方法
/**
* 查看文件大小,递归
*
* @param file
* @return
*/
public static long getFileSize(File file) {
long size = 0;
if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
size = size + getFileSize(files[i]);
}
} else {
size = size + file.length();
}
return size;
}
/**
* 删除某个文件夹,递归
*
* @param filePath
*/
public static void deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {//如果文件存在,并且是文件夹
file.delete();
} else if (file.exists() && file.isDirectory()) {//如果文件是文件夹
File[] childFiles = file.listFiles();
if (childFiles == null || childFiles.length == 0)
return;
for (int i = 0; i < childFiles.length; i++) {
deleteFile(childFiles[i].getPath());
}
}
}
/**
* 文件重命名
*
* @param path 文件目录
* @param oldname 原来的文件名
* @param newname 新文件名
*/
public static void renameFile(String path, String oldname, String newname) {
if (!oldname.equals(newname)) {//新的文件名和以前文件名不同时,才有必要进行重命名
File oldfile = new File(path + "/" + oldname);
if (!oldfile.exists()) {
return;//文件不存在
}
File newfile = new File(path + "/" + newname);
if (newfile.exists()) {//若在该目录下已经有一个文件和新文件名相同,则重命名成(2)
//已经存在!
newfile = new File(path + "/" + newname + "(2)");
oldfile.renameTo(newfile);
} else {
oldfile.renameTo(newfile);
}
}
}
/**
* 获得SD卡总大小
*/
public static String getSDAllSize(Context context) {
File path = getSDFile();
StatFs stat = new StatFs(path.getPath());
long blockSize = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
} else {
blockSize = stat.getBlockSize();
}
long totalBlocks = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
totalBlocks = stat.getBlockCountLong();
} else {
totalBlocks = stat.getBlockCount();
}
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
/**
* 获得sd卡剩余容量,即可用大小
*/
public String getSDRemainingSize(Context con) {
File path = getSDFile();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(con, blockSize * availableBlocks);
}