- android的文件操作要有权限:
- view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> - SD卡下的文件操作:
- 1、判断SD卡是否插入
- view plaincopy to clipboardprint?
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); - 2、获得sd卡根目录:
- view plaincopy to clipboardprint?
File skRoot = Environment.getExternalStorageDirectory();
File skRoot = Environment.getExternalStorageDirectory(); - 私有目录下的文件操作:
- 1、获得私有根目录:
- view plaincopy to clipboardprint?
File fileRoot = Context.getFilesDir()+"\";
File fileRoot = Context.getFilesDir()+"\"; - 还未整理
- 文件夹或文件夹操作:
- 1、确定或获得文件夹和文件路径
- a、获得文件或文件夹的绝对路径和相对路径。区别
- view plaincopy to clipboardprint?
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对 - b 、获得文件或文件夹的父目录
- view plaincopy to clipboardprint?
String parentPath = File.getParent();
String parentPath = File.getParent(); - c、获得文件或文件夹的名称:
- view plaincopy to clipboardprint?
String Name = File.getName();
String Name = File.getName(); - 2、建立文件或文件夹
- view plaincopy to clipboardprint?
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
- 3、判断是文件或文件夹
- view plaincopy to clipboardprint?
File.isDirectory()
File.isDirectory() - 4、列出文件夹下的所有文件和文件夹名
- view plaincopy to clipboardprint?
File[] files = File.listFiles();
File[] files = File.listFiles(); - 5、修改文件夹和文件名
- view plaincopy to clipboardprint?
File.renameTo(dest);
File.renameTo(dest); - 6、删除文件夹或文件
- view plaincopy to clipboardprint?
File.delete();
File.delete(); - view plaincopy to clipboardprint?
package otheri.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import otheri.io.Input;
import otheri.io.Output;
import android.content.Context;
import android.os.Environment;
public class FileHelper {
private Context context;
private String SDPATH;
private String FILESPATH;
public FileHelper(Context context) {
this.context = context;
SDPATH = Environment.getExternalStorageDirect ory().getPath() + "\";
FILESPATH = this.context.getFilesDir().getPath() + "\";
}
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
public boolean delSDFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
file.delete();
return true;
}
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
public boolean delSDDir(String dirName) {
详细解决方案
Android 资料操作大全
热度:80 发布时间:2016-05-01 11:03:12.0