记录一下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. Android获取程序路径 (/data/data/appname)
  2. 用ant实现自动打包android(二) -- android代码混淆
  3. 修改文件夹权限
  4. ant编译android工程用批处理打包
  5. Android(安卓)学习笔记 Contacts (三)Contacts 查找,增加,更新,删除联
  6. Android(安卓)按Menu弹出菜单
  7. NDK编译Android动态链接库
  8. Android下使用Properties文件保存程序设置
  9. android 删除文件,打开指定的文件类型

随机推荐

  1. [Android Samples视频系列之ApiDemos] Ap
  2. Android(安卓)Studio 中文乱码问题
  3. android视图切换动画:ViewAnimator类及其
  4. 创建SqliteDatabase
  5. 【Android Training - Performance】 -
  6. Android如何保存和读取设置
  7. Android中有用笔记
  8. 在Ubuntu上下载编译安装Android最新内核
  9. Android之UI学习篇十一:ListView控件学习(
  10. Android(安卓)setContentView 实现同一个