在使用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文件和目录的增删复制的更多相关文章

  1. Java——文件及目录File操作

    API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...

  2. Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)

    控制台程序,创建和删除目录以及复制和移动文件. import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOE ...

  3. 【转】java 文件 读取目录下的所有文件(包括子目录)

    转自:http://www.cnblogs.com/pricks/archive/2009/11/11/1601044.html import java.io.File; import java.io ...

  4. cp命令:复制文件和目录

    cp命令:复制文件和目录 [功能说明] cp命令可以理解英文单词copy的缩写,其功能为复制文件和目录. [语法格式] 1 cp [option] [source] [dest] 2 cp [选项] ...

  5. 如何使用命令行编译以及运行java文件

    要想编译和运行java文件,很简单,只需要两个命令: (1) javac:作用:编译java文件:使用方法: javac Hello.java ,如果不出错的话,在与Hello.java 同一目录下会 ...

  6. 在windows下使用cmd命令行对java文件进行编译和执行

    windows下利用cmd命令行可以调用jdk里的javac.exe和java.exe对java文件进行编译和执行,前提是jdk已成功安装并正确配置相关环境变量 相关配置链接:java基础学习总结—— ...

  7. Eclipse中Java文件图标由实心J变成空心J的问题

    在eclipse中空心J的java文件,表示不被包含在项目中进行编译,而是当做资源存在项目中.例如 当是单个文件为空心J的时候 1.右击该文件 -- >BuildPath -->Inclu ...

  8. Linux文件和目录操作管理命令

    1.pwd:显示工作目录路径 -p:显示实际物理路径 -l:显示链接路径 2.cd:更改工作目录路径 cd:进入用户主目录 cd~:进入用户主目录 cd-:返回进入此目录之前所在的目录 cd..:返回 ...

  9. 第7章 Linux文件与目录管理

    目录与路径 相对路径与绝对路径 目录的相关操作 . 代表此层目录 .. 代表上层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的文件夹 ~account 代表accou ...

随机推荐

  1. Tcl之Read files for synthesis

    The following file is to read all design files into syntehsis tool automatically, like Cadence RTL C ...

  2. HDU_2112_最短路

    题目链接:http://acm.hdu.edu.cn/status.php?user=l1526789512&pid=2112&status=5 HDU Today Time Limi ...

  3. java基础学习之内存分析(栈、堆、方法区)

    栈存放:会为每个方法(包括构造函数)开辟一个栈指针,方法执行完毕后,会自动退出,并释放空间,主要每个方法中的存放局部变量 局部变量   先进后出 自下而上存储 方法执行完毕 自动释放空间 堆: 存放n ...

  4. JAVA基础——链表结构之双端链表

    双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用 如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双 ...

  5. JAVA基础——构造函数方法总结(有参构造和无参构造)

    使用构造器时需要记住: 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回 ...

  6. xmpp使用经验

    IM 标准协议有XMPP\IMPP\PRIM\SIP(SIMPLE)等,其中XMPP基于XML的协议,具备了很好的扩展性(依靠XML的域名空间)并且可以建立在TLS上使用SASL认证. 1.文件比如图 ...

  7. Scoop - 在Windows命令行上进行程序安装

    2019-01-28  22:49:21 资料来源自Scoop官方网站以及github上的帮助文档 如果有疑惑或者觉得文章有错误请留言以帮助改正 补充内容(2019-04-09 21:11:36):不 ...

  8. linux cmp-比较两个文件是否有差异

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 cmp命令用来比较两个文件是否有差异.当相互比较的两个文件完全一样时,则该指令不会显示任何信息.若发现有差异,预设会标示出第一个不通 ...

  9. session对象的使用

    session对象的使用 制作人:全心全意 session在网络中被称为会话.由于HTTP协议是一种无状态协议,也就是当一个客户向服务器发出请求,服务器接收请求,并返回响应后,该连接就结束了,而服务器 ...

  10. cmd界面中断一个程序快捷键 ctrl+c

    cmd界面中断一个程序快捷键   ctrl+c