全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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文件目录增删复制  # java文件增删复制  # java目录增删复制  # java复制文件的4种方式及拷贝文件到另一个目录下的实例代码  # Java实现文件或文件夹的复制到指定目录实例  # java读取excel文件并复制(copy)文件到指定目录示例  # java怎么创建目录(删除/修改/复制目录及文件)代码实例  # java将一个目录下的所有数据复制到另一个目录下  # 不存在  # 写了  # 希望大家  # 大家分享  # 大家多多  # 小工具  # mkdir  # throw  # filePath  # catch  # exists  # void  # static  # newFolder  # myFolder  # String  # print  # printWriter  # flush  # close 


相关文章: MySQL查询结果复制到新表的方法(更新、插入)  如何用腾讯建站主机快速创建免费网站?  英语简历制作免费网站推荐,如何将简历翻译成英文?  制作网站怎么制作,*游戏网站怎么搭建?  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  建站主机选购指南:核心配置优化与品牌推荐方案  C++时间戳转换成日期时间的步骤和示例代码  网站制作软件有哪些,制图软件有哪些?  表情包在线制作网站免费,表情包怎么弄?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  微信推文制作网站有哪些,怎么做微信推文,急?  如何快速生成可下载的建站源码工具?  如何在阿里云域名上完成建站全流程?  建站之星Pro快速搭建教程:模板选择与功能配置指南  建站上传速度慢?如何优化加速网站加载效率?  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  Java解压缩zip - 解压缩多个文件或文件夹实例  家具网站制作软件,家具厂怎么跑业务?  电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?  长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?  网站代码制作软件有哪些,如何生成自己网站的代码?  5种Android数据存储方式汇总  如何设计高效校园网站?  如何在万网开始建站?分步指南解析  济南网站制作的价格,历城一职专官方网站?  招贴海报怎么做,什么是海报招贴?  整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?  C#怎么使用委托和事件 C# delegate与event编程方法  如何在企业微信快速生成手机电脑官网?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  北京企业网站设计制作公司,北京铁路集团官方网站?  建站之星如何修改网站生成路径?  浅析上传头像示例及其注意事项  成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?  创业网站制作流程,创业网站可靠吗?  移民网站制作流程,怎么看加拿大移民官网?  儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?  建站主机类型有哪些?如何正确选型  网站制作知乎推荐,想做自己的网站用什么工具比较好?  建站主机是否等同于虚拟主机?  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  在线制作视频的网站有哪些,电脑如何制作视频短片?  如何自定义建站之星网站的导航菜单样式?  javascript中的try catch异常捕获机制用法分析  枣阳网站制作,阳新火车站打的到仙岛湖多少钱?  如何自定义建站之星模板颜色并下载新样式?  如何在宝塔面板创建新站点?  制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?  如何快速搭建虚拟主机网站?新手必看指南  如何在腾讯云服务器上快速搭建个人网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。