Android , Java 文件操作类

来源:互联网 发布:手机淘宝怎么联系小二 编辑:程序博客网 时间:2024/06/02 13:49
// 2012-4-18下午07:49:44package com.su.funycard.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.util.Log;import com.su.funycard.bean.PicFileBean;public class FileUtil {// 复制文件public static void copyFile(String ssourceFile, String stargetFile) {String folderpath = stargetFile.substring(0,stargetFile.lastIndexOf("/"));FileUtil.createFolder(folderpath);File sourceFile = new File(ssourceFile);File targetFile = new File(stargetFile);try {FileInputStream input = new FileInputStream(sourceFile);BufferedInputStream inBuff = new BufferedInputStream(input);// 新建文件输出流并对它进行缓冲FileOutputStream output = new FileOutputStream(targetFile);BufferedOutputStream outBuff = new BufferedOutputStream(output);// 缓冲数组byte[] b = new byte[1024 * 5];int len;while ((len = inBuff.read(b)) != -1) {outBuff.write(b, 0, len);}// 刷新此缓冲的输出流outBuff.flush();// 关闭流inBuff.close();outBuff.close();output.close();input.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static int getMyEleCounts(String path) {File file = new File(path);if (file.isDirectory()) {return file.list().length;}return 0;}public static List<PicFileBean> getImageNames(String folderPath) {List<PicFileBean> pictureFiles = new ArrayList<PicFileBean>();File file = new File(folderPath);if (file.isDirectory()) {String[] filelist = file.list();for (int i = 0; i < filelist.length; i++) {File file02 = new File(folderPath + "/" + filelist[i]);if (!file02.isDirectory()) {// if (isImageFile(file02.getName())) {PicFileBean pictureFile = new PicFileBean(filelist[i],file02.getAbsolutePath());pictureFiles.add(pictureFile);// }}}}return pictureFiles;}private static boolean isImageFile(String fileName) {String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("jpg")) {return true;} else if (fileEnd.equalsIgnoreCase("png")) {return true;} else if (fileEnd.equalsIgnoreCase("bmp")) {return true;} else {return false;}}/** * 复制asset文件夹下的文件到sd卡上 *  * @param context *            上下文 * @param assetDir *            asset指定文件夹的路径 * @param dir *            sd卡指定文件夹的路径 */public static void copyAssets(Context context, String assetDir, String dir) {String[] files;try {files = context.getResources().getAssets().list(assetDir);} catch (IOException e1) {return;}File mWorkingPath = new File(dir);// if this directory does not exists, make one.if (!mWorkingPath.exists()) {if (!mWorkingPath.mkdirs()) {}}for (int i = 0; i < files.length; i++) {try {String fileName = files[i];// we make sure file name not contains '.' to be a folder.if (!fileName.contains(".")) {if (0 == assetDir.length()) {// 这里使用的递归copyAssets(context, fileName, dir + fileName + "/");} else {copyAssets(context, assetDir + "/" + fileName, dir+ fileName + "/");}continue;}File outFile = new File(mWorkingPath, fileName);if (outFile.exists()) {continue;}InputStream in = null;if (0 != assetDir.length()) {in = context.getAssets().open(assetDir + "/" + fileName);} else {in = context.getAssets().open(fileName);}OutputStream out = new FileOutputStream(outFile);// Transfer bytes from in to outbyte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}} catch (Exception e) {e.printStackTrace();}}}public static boolean existFile(String path) {File file = new File(path);if (file.exists()) {return true;} else {return false;}}public static void createFolder(String path) {File file = new File(path);if (!file.exists()) {file.mkdirs();}}/** * 删除文件夹 *  * @param folderPath */public static void delFolder(String folderPath) {try {delAllFile(folderPath); // 删除完里面所有内容String filePath = folderPath;filePath = filePath.toString();java.io.File myFilePath = new java.io.File(filePath);myFilePath.delete(); // 删除空文件夹} catch (Exception e) {System.out.println("删除文件夹操作出错");e.printStackTrace();}}public static void delAllFile(String path) {File file = new File(path);if (!file.exists()) {return;}if (!file.isDirectory()) {return;}String[] tempList = file.list();File temp = null;for (int i = 0; i < tempList.length; i++) {if (path.endsWith(File.separator)) {temp = new File(path + tempList[i]);} else {temp = new File(path + File.separator + tempList[i]);}if (temp.isFile()) {temp.delete();}if (temp.isDirectory()) {delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件delFolder(path + "/" + tempList[i]);// 再删除空文件夹}}}}


package com.su.filesystem;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import android.util.Log;public class FileUtil {public int j = 0;public static ArrayList<File> list = new ArrayList<File>();// 根据路径path获取该路径下的所有文件的文件名public String[] getFileNames(String path) {File file = new File(path);return file.list();}public void renameFile(String oldFile, String newFile) {File file = new File(oldFile);file.renameTo(new File(newFile));}public void createFolder(String sfile) {File file = new File(sfile);file.mkdirs();}public void createFile(String sfile) throws IOException {File file = new File(sfile);file.createNewFile();}// 根据文件后缀名判断是不是txt文件public boolean isTxtFile(String fileName) {File file = new File(fileName);String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("txt")) {return true;} else {return false;}}public boolean isMp3File(String fileName) {String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("mp3")) {return true;} else {return false;}}public boolean isImageFile(String fileName) {String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("jpg")) {return true;} else if (fileEnd.equalsIgnoreCase("png")) {return true;}else if (fileEnd.equalsIgnoreCase("gif")) {return true;}else{return false;}}// 判断是什么格式的文件public int isWhichFile(String fileName) {String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("txt")) {return R.drawable.file_txt;} else if (fileEnd.equalsIgnoreCase("lrc")) {return R.drawable.file_lrc;} else {return R.drawable.file_unknown;}}public void copyFolder(String oldPath, String newPath) {try {(new File(newPath)).mkdirs();// 如果文件夹不存在 则建立新文件夹File a = new File(oldPath);String[] file = a.list();File temp = null;for (int i = 0; i < file.length; i++) {if (oldPath.endsWith(File.separator)) {temp = new File(oldPath + file[i]);} else {temp = new File(oldPath + File.separator + file[i]);}if (temp.isFile()) {FileInputStream input = new FileInputStream(temp);FileOutputStream output = new FileOutputStream(newPath+ "/" + (temp.getName()).toString());byte[] b = new byte[1024 * 5];int len;while ((len = input.read(b)) != -1) {output.write(b, 0, len);}output.flush();output.close();input.close();}if (temp.isDirectory() && !newPath.contains(oldPath)) {// 如果是子文件夹// 还要判断是否是复制在本目录下如果复制本目录下// 那只递归一次否则死循环copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);}}} catch (Exception e) {System.out.println("复制整个文件夹内容操作出错");e.printStackTrace();}}public boolean copyFile(String frompath, String topath) {try {File fromFile = new File(frompath);File toFile = new File(topath);FileInputStream fosfrom = new FileInputStream(fromFile);FileOutputStream fosto = new FileOutputStream(toFile);byte bt[] = new byte[1024];int c;while ((c = fosfrom.read(bt)) > 0) {fosto.write(bt, 0, c); // 将内容写到新文件当中}fosfrom.close();fosto.close();} catch (Exception ex) {Log.e("readfile", ex.getMessage());}return true;}public boolean delFile(String path) {File file = new File(path);return file.delete();// return true;}public void delFolder(String folderPath) {try {delAllFile(folderPath); // 删除完里面所有内容String filePath = folderPath;filePath = filePath.toString();java.io.File myFilePath = new java.io.File(filePath);myFilePath.delete(); // 删除空文件夹} catch (Exception e) {System.out.println("删除文件夹操作出错");e.printStackTrace();}}/** * 删除文件夹里面的所有文件 *  * @param path *  */public void delAllFile(String path) {File file = new File(path);if (!file.exists()) {return;}if (!file.isDirectory()) {return;}String[] tempList = file.list();File temp = null;for (int i = 0; i < tempList.length; i++) {if (path.endsWith(File.separator)) {temp = new File(path + tempList[i]);} else {temp = new File(path + File.separator + tempList[i]);}if (temp.isFile()) {temp.delete();}if (temp.isDirectory()) {delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件delFolder(path + "/" + tempList[i]);// 再删除空文件夹}}}public static ArrayList<File> getAllFiles(File root) {File files[] = root.listFiles();if (files != null)for (File f : files) {if (f.isDirectory()) {getAllFiles(f);} else {if (f.getName().indexOf(".lrc") > 0)list.add(f);}}return list;}}