麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

java 文件操作工具類

2019-11-06 05:26:48
字體:
來源:轉載
供稿:網友
/** * 文件工具類 * */public class FileUtil { PRivate FileUtil() { } /** * 創建文件 * @param in * @param filePath */ public static void createFile(InputStream in, String filePath) { if(null == in){ throw new RuntimeException("創建文件失敗: 入參inputstream為空"); } if(null == filePath){ throw new RuntimeException("創建文件失敗: 入參filePath為空"); } int potPos = filePath.lastIndexOf('/') + 1; String folderPath = filePath.substring(0, potPos); createFolder(folderPath); FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(filePath); byte[] by = new byte[1024]; int c; while ((c = in.read(by)) != -1) { outputStream.write(by, 0, c); } outputStream.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } /** * java從文件路徑中獲取文件名 * * @param filePath * @return */ public static String getPathFileName(String filePath) { if ((filePath != null) && (filePath.length() > 0)) { filePath = filePath.replaceAll("http:////", "/"); int dot = filePath.lastIndexOf("/"); if ((dot > -1) && (dot < (filePath.length()))) { return filePath.substring(dot + 1, filePath.length()); } } return filePath; } /** * 把內容寫入文件 * * @param filePath * @param fileContent */ public static void write(String filePath, String fileContent) { try { FileOutputStream fo = new FileOutputStream(filePath); OutputStreamWriter out = new OutputStreamWriter(fo, "UTF-8"); out.write(fileContent); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 按編碼格式讀取文件內容 默認是UTF-8編碼 * * @param filePath * @return */ public static String read(String filePath, String code) { if (code == null || code.equals("")) { code = "UTF-8"; } String fileContent = ""; File file = new File(filePath); try { InputStreamReader read = new InputStreamReader(new FileInputStream(file), code); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { fileContent = fileContent + line + "/n"; } read.close(); read = null; reader.close(); read = null; } catch (Exception ex) { ex.printStackTrace(); fileContent = ""; } return fileContent; } /** * 判斷文件是否存在 * * @param filePath */ public static boolean exist(String filepath) { File file = new File(filepath); return file.exists(); } /** * 創建文件夾 * * @param filePath */ public static void createFolder(String filePath) { try { File file = new File(filePath); if (!file.exists()) { file.mkdirs(); } } catch (Exception ex) { System.err.println("Make Folder Error:" + ex.getMessage()); } } /** * 重命名文件、文件夾 * * @param from * @param to */ public static void renameFile(String from, String to) { try { File file = new File(from); if (file.exists()) { file.renameTo(new File(to)); } } catch (Exception ex) { System.err.println("Rename File/Folder Error:" + ex.getMessage()); } } /** * 得到文件的擴展名(即.后面的后綴名) * * @param fileName * @return */ public static String getFileExt(String fileName) { int potPos = fileName.lastIndexOf('.') + 1; String type = fileName.substring(potPos, fileName.length()); return type; } /** * 通過File對象創建文件 * * @param file * @param filePath */ public static void createFile(File file, String filePath) { int potPos = filePath.lastIndexOf('/') + 1; String folderPath = filePath.substring(0, potPos); createFolder(folderPath); FileOutputStream outputStream = null; FileInputStream fileInputStream = null; try { outputStream = new FileOutputStream(filePath); fileInputStream = new FileInputStream(file); byte[] by = new byte[1024]; int c; while ((c = fileInputStream.read(by)) != -1) { outputStream.write(by, 0, c); } } catch (IOException e) { e.getStackTrace().toString(); } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 將輸入流轉換為字符串 * * @param stream */ public static String readStreamToString(InputStream stream) { String fileContent = ""; try { InputStreamReader read = new InputStreamReader(stream, "utf-8"); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { fileContent = fileContent + line + "/n"; } read.close(); read = null; reader.close(); read = null; } catch (Exception ex) { fileContent = ""; } return fileContent; } /** * 復制整個文件夾內容 * * @param oldPath * String 原路徑 * @param newPath * String 目標路徑 * @return boolean */ public static void copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); // 如果文件夾不存在 則建立新文件夾 File oldFile = new File(oldPath); String[] file = oldFile.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()) {// 如果是子文件夾 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { System.out.println("復制整個文件夾內容操作出錯"); e.printStackTrace(); } } /** * 刪除文件夾 * * @param filePathAndName * String 文件夾路徑及名稱 如c:/fqf * @param fileContent * String * @return boolean */ 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(); } } /** * 刪除文件夾里面的所有文件 * * @param path * String 文件夾路徑 如 c:/fqf */ 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]);// 再刪除空文件夾 } } } /** * 移動文件到指定目錄 * * @param oldPath * String 如:c:/fqf.txt * @param newPath * String 如:d:/fqf.txt */ public static void moveFolder(String oldPath, String newPath) { copyFolder(oldPath, newPath); delFolder(oldPath); }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 老师你怎么会在这第2季出现 | 国产一级毛片不卡 | 黑人一级片视频 | 亚洲一区二区三区在线免费观看 | 国产精品久久久久久久久粉嫩 | 在线免费观看毛片视频 | 黄色影院在线观看视频 | 久久精品之 | 精品国产中文字幕 | 日本aaaa片毛片免费观看视频 | 日韩大片在线永久观看视频网站免费 | 日韩黄网站 | 亚洲国产美女视频 | 在线观看视频亚洲 | 久久精品中文字幕 | 久草高清视频 | 久久亚洲综合色 | 欧美三级短视频 | 91专区在线观看 | 亚洲成人福利在线观看 | 欧美一级黄 | 欧美成人精品一区 | 精品黑人一区二区三区国语馆 | 午夜男人在线观看 | 国产精品久久久在线观看 | 欧美性生交xxxxx免费观看 | 久久精品免费国产 | 久久精品欧美一区二区 | 羞羞视频免费观看入口 | 久久99国产精品久久 | 鲁丝一区二区三区不属 | 久久免费视频8 | 羞羞的小视频 | 成人毛片100免费观看 | 毛片一级片 | 欧美在线观看视频一区二区 | 91精品国产综合久久久欧美 | 在线成人免费观看视频 | 福利四区 | 成人三级在线播放 | 精品国产乱码久久久久久丨区2区 |