欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java文件和目录的增删复制

程序员文章站 2023-12-15 08:24:16
在使用java进行开发时常常会用到文件和目录的增删复制等方法。我写了一个小工具类。和大家分享,希望大家指正: package com.wangpeng.utill...

在使用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");
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: