android的文件、目录操作(二)
2011-03-18 18:01:13| 分类: 工作学习 | 标签: |字号大中小 订阅
- 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.getExternalStorageDirectory().getPath() + "\\";
- FILESPATH = this.context.getFilesDir().getPath() + "\\";
- }
- /**
- * 在SD卡上创建文件
- *
- * @throws IOException
- */
- public File creatSDFile(String fileName) throws IOException {
- File file = new File(SDPATH + fileName);
- file.createNewFile();
- return file;
- }
- /**
- * 删除SD卡上的文件
- *
- * @param fileName
- */
- public boolean delSDFile(String fileName) {
- File file = new File(SDPATH + fileName);
- if (file == null || !file.exists() || file.isDirectory())
- return false;
- file.delete();
- return true;
- }
- /**
- * 在SD卡上创建目录
- *
- * @param dirName
- */
- public File creatSDDir(String dirName) {
- File dir = new File(SDPATH + dirName);
- dir.mkdir();
- return dir;
- }
- /**
- * 删除SD卡上的目录
- *
- * @param dirName
- */
- public boolean delSDDir(String dirName) {
- File dir = new File(SDPATH + dirName);
- return delDir(dir);
- }
- /**
- * 修改SD卡上的文件或目录名
- *
- * @param fileName
- */
- public boolean renameSDFile(String oldfileName, String newFileName) {
- File oleFile = new File(SDPATH + oldfileName);
- File newFile = new File(SDPATH + newFileName);
- return oleFile.renameTo(newFile);
- }
- /**
- * 拷贝SD卡上的单个文件
- *
- * @param path
- * @throws IOException
- */
- public boolean copySDFileTo(String srcFileName, String destFileName)
- throws IOException {
- File srcFile = new File(SDPATH + srcFileName);
- File destFile = new File(SDPATH + destFileName);
- return copyFileTo(srcFile, destFile);
- }