首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

java 根本的文件操作

2012-09-16 
java 基本的文件操作好吧,我基础真的很不扎实。基本的文件操作都不熟,那么多总结,多积累吧!直接上传工具类,

java 基本的文件操作
好吧,我基础真的很不扎实。

基本的文件操作都不熟,那么多总结,多积累吧!

直接上传工具类,不多说,好多东西都是自己程序里面,没怎么改。

public final static String FILE_NAME="your filepath";public static void writeDefaultFolderFile(List<FileInfo> listFoler) throws IOException{Log.e("writeInfo", "writeInfo");OutputStream fis = new FileOutputStream(new File(FILE_NAME));StringBuffer sb = new StringBuffer();for(int i = 0;i < listFoler.size();i++){sb.append( listFoler.get(i).folderName);sb.append("--");sb.append(listFoler.get(i).folderPath);sb.append("\r\n");//Log.e("","write:"+i);}fis.write(sb.toString().getBytes("utf_8"));fis.flush();fis.close(); }/** * 这些函数只能我看咯,你们不知道这是用来做啥的!fileInfo我也没写 - -! */public static List<FileInfo> readDefaultFolderFile(){//List<FileInfo> listFolder = new ArrayList<FileInfo>();Log.e("readInfo","readInfo"); try       { FileReader reader = new FileReader(FILE_NAME); BufferedReader br = new BufferedReader(reader); String item = br.readLine(); if(null == item || item.trim().equals("")){ return null; }  FileInfo folderInfo = null; do { Log.e("readDefaultFolderFile()", "folderItem:"+item);try {folderInfo = new FileInfo();String temp[] = item.split("--");folderInfo.folderName = temp[0];folderInfo.folderPath = temp[1];listFolder.add(folderInfo);} catch (Exception e) {}}while((item = br.readLine()) != null && !item.equals("")); br.close(); reader.close();    }    catch(IOException e)    {     e.printStackTrace();       }return listFolder;}/** *创建文件夹 */public static void createDefaultFolderFile(){Log.e("createPrefFile", "createPrefFile");File f = new File(FILE_NAME);if(f.exists() == false){try {f.createNewFile();} catch (IOException e) {}}}/** * 判断文件是否存在 */public static boolean isDefaultFolderFileExist(){File f = new File(FILE_NAME);if(f.exists() == false){return false;}else{return true;}}/** * 删除文件 */public static void deleteDefaultFolderFile(){File f = new File(FILE_NAME);f.delete();Log.e("deletePrefFile", "deletePrefFile");}public final static String MY_FOLDER_PATH = "/mnt/sdcard/MyFolder";/** * 新建文件夹 */public static void createNewFolder(Context ctx, String folderName) {File folder = new File(MY_FOLDER_PATH +"/"+ folderName);Log.e("bkship", "folderPath:" + folder.getPath());if (!folder.exists()) {boolean createOk = folder.mkdirs();if (createOk) {showToast("文件夹:"+folderName+"已创建。", ctx);} else {showToast("文件夹名称不合法!", ctx);}}}/** * 重命名文件夹 * @param folderPath * @param newFolderName * @param ctx  */public static void renameFolder(String folderPath,String newFolderName, Context ctx) {File folder = new File(folderPath);String path = folder.getAbsolutePath();File newFolder = new File(path.substring(0, path.lastIndexOf("/")+1)+newFolderName);if(folder.renameTo(newFolder) == true){showToast("重命名成功。", ctx);}else{showToast("名称不合法。", ctx);}}  /** * 删除文件夹 * @param ctx * @param folderPath */public static void deleteFolder(Context ctx, String folderPath) {File folder = new File(folderPath);String[] fileList = folder.list();if (isFolderEmpty(folderPath) == true) {if (!folder.delete()) {showToast("Fail,Delete folder", ctx);return;}} else {for (int i = 0; i < fileList.length; i++) {String fileName = fileList[i];String filePath = folderPath + File.separator + fileName;File file = new File(filePath);if (file.exists() && file.isFile()) {if (!file.delete()) {showToast("Fail,Delete folder", ctx);return;}} else if (file.exists() && file.isDirectory()) {deleteFolder(ctx, filePath);}}if (!folder.delete()) {showToast("文件夹删除失败。", ctx);return;}}showToast("删除成功。", ctx);}/** * 判断文件夹是否为空 *  * @param folderPath * @return */public static boolean isFolderEmpty(String folderPath) {File folder = new File(folderPath);String[] fileList = folder.list();if (null == fileList || fileList.length == 0) {return true;}return false;}/** * 拷贝文件 */static String filePathForCopy;public static void copyFile(String filePath) {filePathForCopy = filePath;}/** * 粘贴文件操作 *  * @param filePath * @param folderPath * @throws IOException */public static void plastFile(String filePath, String folderPath)throws IOException {byte[] buffer = new byte[1024 * 8];File file = new File(filePath);FileInputStream fis = new FileInputStream(file);String fileName = file.getName();Log.e("fileName", "fileName:" + fileName);File newFile = new File(folderPath + File.separator + fileName);if (newFile.exists()) {newFile.delete();}newFile.createNewFile();FileOutputStream fos = new FileOutputStream(newFile);int read = 0;while ((read = fis.read(buffer)) > 0) {fos.write(buffer, 0, read);fos.flush();}if (null != fis) {fis.close();}if (null != fos) {fos.close();}buffer = null;}/** * 移动文件到指定目录 *  * @param filePath * @param folderPath */public static void moveFile(String filePath, String folderPath) {try {plastFile(filePath, folderPath);} catch (IOException e) {e.printStackTrace();}File file = new File(filePath);if (file.delete()) {//Log.e("bkship", "Successfully,move file to folder!");}}/** * showToast * @param str * @param ctx */public static void showToast(String str, Context ctx) {Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();}/** * 将MyFolder文件夹中文件夹信息放到List中 * @return */public static List<FileInfo> getMyFolderList(){List<FileInfo> listFolder = new ArrayList<FileInfo>();File folder = new File(MY_FOLDER_PATH);if(folder.exists() == false){booleancreateOk = folder.mkdirs();Log.e("","creat MyFolder:"+createOk);return listFolder;}File[] files = folder.listFiles();FileInfo fileInfo;if(files != null){for(int i = 0;i<files.length;i++){File file = files[i];if(file.isDirectory() == true){fileInfo = new FileInfo();fileInfo.folderName = file.getName();fileInfo.folderPath = file.getAbsolutePath();fileInfo.isFolder = true;listFolder.add(fileInfo);}}}return listFolder;}public static boolean isFolderNameExist(String str){File folder = new File(MY_FOLDER_PATH);File[] files = folder.listFiles();for(int i = 0;i<files.length;i++){if(files[i].getName().equals(str)){return true;}}return false;}

热点排行