首页 > 编程 > 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("////", "/"); 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); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表