记录一下Android常用的文件操作

1.自动级联创建文件
/**
* 自动级联创建一个文件
*
* @param filePath
* @return
* @throws IOException
* @throws
*/
public static File createFile(String filePath) throws IOException {
File file = null;
if (filePath != null) {
file = new File(filePath);
// 如果文件不存在就创建一个
if (!file.exists()) {
// 如果路径不存在,则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
}
return file;
}

2.按照路径删除一个文件
/**
* 按照路径删除一个文件
*
* @param filePath
* @throws
*/
public static void deleteFile(String filePath) {
if (filePath != null) {
File file = new File(filePath);
// file.deleteOnExit();此方法有时无效
if(file.exists()){
file.delete();
}
}
}

3.删除一个文件
/**
* 删除一个文件
*
* @param filePath
* @throws
*/
public static void deleteFile(File file) {
if (file != null) {
// file.deleteOnExit();
if(file.exists()){
file.delete();
}
}
}

4.保存一个字符串到文件里面去
/**
* 保存一个字符串到文件里面去
*
* @param filePath
* @param content
* @throws IOException
* @throws
*/
public static void saveString(String filePath, String content)
throws IOException {
if (filePath != null) {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
// 创建文件失败,则退出操作
if (!file.exists()) {
return;
}
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
bw = null;
fw.close();
fw = null;
}
}

5.向文件追加写入
/**
* 追加文件写入
*
* @param filePath
* @param content
* @throws IOException
* @throws
*/
public static void addString(String filePath, String content)
throws IOException {
if (filePath != null) {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
// 文件追加写入
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
bw = null;
fw.close();
fw = null;
}
}

6.从本地文件中获取字符串
/**
* 从本地文件中获取字符串
*
* @param filePath
* @return
* @throws IOException
* @throws
*/
public static String getString(String filePath) throws IOException {
String result = null;
if (filePath != null) {
StringBuffer sb = null;
File file = new File(filePath);
if (!file.exists()) {
return null;
}
FileReader fw = new FileReader(file);
BufferedReader br = new BufferedReader(fw);
sb = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
fw.close();
result = sb.toString();
}
return result;
}

7.创建文件夹
/**
* 创建文件夹
*
* @param dirPath
* @throws
*/
public static void createDir(String dirPath) {
if (dirPath != null) {
File file = new File(dirPath);
// 如果不存在,并且不是文件类型的,就认为是文件夹
if (!file.isFile() && !file.exists()) {
// 级联创建文件夹
file.mkdirs();
}
}
}

8.删除文件夹
/**
* 删除文件夹
*
* @param dirPath
* @throws
*/
public static void deleteDir(String dirPath) {
if (dirPath != null) {
File file = new File(dirPath);
// file.deleteOnExit();//此方法失效
if(file.exists()){
file.delete();
}
}
}

9.复制文件,src表示源文件路径,targ表示目标文件路径
/**
* 复制文件,src表示源文件路径,targ表示目标文件路径
*
* @throws IOException
* @throws FileNotFoundException
* @param src
* @param targ
* @throws
*/
public static void copyFile(String src, String targ) throws IOException {
if (src != null && targ != null) {
File srcFile = new File(src);
File targFile = new File(targ);
if (srcFile.exists()) {
if (!targFile.exists()) {
targFile.createNewFile();
}
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targFile);
int len = 0;
byte[] buf = new byte[2048];
while ((len = fis.read(buf, 0, 2048)) > 0) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
fos = null;
fis.close();
fis = null;
}
}
}

10.从文件地址截取文件名称
/**
* 从文件地址截取文件名称
*
* @param url
* @return
*/
public static final String getFilenameFromUrl(String url) {
if (url == null) {
return null;
}
int index = url.lastIndexOf(‘/’);
if (index < 0) {
return url;
}
if (index == (url.length() - 1)) {
return “”;
}
int pos = url.lastIndexOf(‘.’);
if (pos < 0) {
return url.substring(index + 1);
}
else {
return url.substring(index + 1, pos);
}
}

11.判断一个本地文件是否存在
/**
* 判断一个本地文件是否存在
*
* @param filePath
* @return
* @throws
*/
public static boolean isExistFile(String filePath) {
boolean result = false;
if (filePath != null) {
File file = new File(filePath);
if (file.exists()) {
result = true;
}
}
return result;
}

12.递归删除所有文件
/**
* 递归删除所有文件
* @param file
*/
public static void deleteAll(File file) {
if (file == null) {
return;
}
File[] fileList = file.listFiles();
if(fileList != null){
for (File tempFile : fileList) { // 递归删除文件
if (tempFile.isFile()) {
tempFile.delete();
} else {
deleteAll(tempFile);
}
}
}
if (file.exists() && file.isDirectory()) { // 删除文件夹
file.delete();
}
}

13.解压文件到指定目录
/**
* 解压文件到指定目录
* @param zipPath
* @param descDir
*/
@SuppressWarnings(“rawtypes”)
public static void unZipFiles(String zipPath,String descDir)throws IOException{
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(new File(zipPath));
for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
//Log.i(“space”,”–>>utf-8转换前”+entry.getName());
String zipEntryName = new String(entry.getName().getBytes(), “utf-8”);
//Log.i(“space”,”–>>utf-8转换后”+zipEntryName);
InputStream in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll(“\*”, “/”);;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf(‘/’)));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
//输出文件路径信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
}

如有错误欢迎指出,谢谢

更多相关文章

  1. 修改文件夹权限
  2. Android下使用Properties文件保存程序设置
  3. android解析xml文件 Android DOM解析XML之全球实时地震信息列表
  4. android 删除文件,打开指定的文件类型
  5. Android Studio2.0引入so文件(亲测)
  6. eclipse创建android项目,无法正常预览布局文件
  7. Android lint 删除无用图片文件和配置文件
  8. android 开发-数据存储之文件存储
  9. [Android] 该文件包与具有同一名称的现有文件包存在冲突

随机推荐

  1. Android游戏开发之旅三 View类详解
  2. android源码编译报错:prebuilts/misc/linu
  3. Android(安卓)开发知识体系
  4. Android中万能的BaseAdapter的使用
  5. android 完全退出实现
  6. Android的Log捕获与有效性验证及异常初步
  7. Android(安卓)UI开发第八篇——ViewFlipp
  8. Android(安卓)获取手机中微信聊天记录并
  9. Android(安卓)带进度的圆形进度条
  10. Android(安卓)Opencv例子Face-detection