java文件和目录的增删复制详解
java
复制
这篇文章主要为大家详细介绍了java文件和目录的增删复制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在使用java进行开发时常常会用到文件和目录的增删复制等方法。我写了一个小工具类。和大家分享,希望大家指正:
package com.wangpeng.utill; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.PrintWriter; /** * @author wangpeng * */ public class ToolOfFile { /** * 创建目录 * * @param folderPath * 目录目录 * @throws Exception */ public static void newFolder(String folderPath) throws Exception { try { java.io.File myFolder = new java.io.File(folderPath); if (!myFolder.exists()) { myFolder.mkdir(); } } catch (Exception e) { throw e; } } /** * 创建文件 * * @param filePath * 文件路径 * @throws Exception */ public static void newFile(String filePath) throws Exception { try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } } catch (Exception e) { throw e; } } /** * 创建文件,并写入内容 * * @param filePath * 文件路径 * @param fileContent * 被写入的文件内容 * @throws Exception */ public static void newFile(String filePath, String fileContent) throws Exception { // 用来写入字符文件的便捷类 FileWriter fileWriter = null; // 向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自己主动行刷新的新 PrintWriter printWriter = null; try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } fileWriter = new FileWriter(myFile); printWriter = new PrintWriter(fileWriter); printWriter.print(fileContent); printWriter.flush(); } catch (Exception e) { throw e; } finally { if (printWriter != null) { printWriter.close(); } if (fileWriter != null) { fileWriter.close(); } } } /** * 复制文件 * * @param oldPath * 被拷贝的文件 * @param newPath * 复制到的文件 * @throws Exception */ public static void copyFile(String oldPath, String newPath) throws Exception { InputStream inStream = null; FileOutputStream outStream = null; try { int byteread = 0; File oldfile = new File(oldPath); // 文件存在时 if (oldfile.exists()) { inStream = new FileInputStream(oldfile); outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } /** * 复制文件 * @param inStream 被拷贝的文件的输入流 * @param newPath 被复制到的目标 * @throws Exception */ public static void copyFile(InputStream inStream, String newPath) throws Exception { FileOutputStream outStream = null; try { int byteread = 0; outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } /** * 复制目录 * * @param oldPath * 被复制的目录路径 * @param newPath * 被复制到的目录路径 * @throws Exception */ public static void copyFolder(String oldPath, String newPath) throws Exception { try { (new File(newPath)).mkdirs(); // 假设目录不存在 则建立新目录 File a = new File(oldPath); String[] file = a.list(); File tempIn = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { tempIn = new File(oldPath + file[i]); } else { tempIn = new File(oldPath + File.separator + file[i]); } if (tempIn.isFile()) { copyFile(tempIn.getAbsolutePath(), newPath + "/" + (tempIn.getName()).toString()); } else if (tempIn.isDirectory()) {// 假设是子目录 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { throw e; } } /** * 删除文件 * * @param filePathAndName */ public static void delFileX(String filePathAndName) { File myDelFile = new File(filePathAndName); myDelFile.delete(); } /** * 删除目录 * * @param path */ public static void delForder(String path) { File delDir = new File(path); if (!delDir.exists()) { return; } if (!delDir.isDirectory()) { return; } String[] tempList = delDir.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(); } else if (temp.isDirectory()) { // 删除完里面全部内容 delForder(path + "/" + tempList[i]); } } delDir.delete(); } public static void main(String[] args) { String oldPath = "F:/test/aaa/"; String newPath = "F:/test/bbb/"; try { // ToolOfFile.newFolder("F:/test/hello/"); // ToolOfFile.newFile("F:/test/hello/world.txt","我爱你,the world! "); ToolOfFile.copyFolder(oldPath, newPath); // ToolOfFile.delForder("F:/test/hello"); } catch (Exception e) { e.printStackTrace(); } System.out.println("OK"); } }
登录后复制
以上是java文件和目录的增删复制详解的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处
