在使用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. Sql server 本地临时表、全局临时表的区别

    创建了本地临时表#Tmp.全局临时表##Tmp: 本地临时表: 取名以#开头.如:#Tmp(会自动加上一串后缀) 只有当前的连接可以访问 连接关闭后,本地临时表自动释放 全局临时表: 取名以##开头. ...

  2. 【Python-2.7】list类型

    list是Python中的一种数据类型,也就是"列表".在Python中我们可以对list类型进行插入,删除,修改等操作. ##新建list类型 >>> ball ...

  3. CAD与用户互在图面上得到一个矩形框(com接口VB语言)

    主要用到函数说明: MxDrawXCustomFunction::ExApp_CutDwg 与用户互在图面上得到一个矩形框,详细说明如下: 参数 说明 IN DOUBLE dX1 保存范围的左下角位置 ...

  4. vue基础---条件渲染

    (1)v-if条件渲染 v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. 可以用 v-else 添加一个“else 块”: ①表达式 <di ...

  5. Microsoft SQL Server 存储过程

    Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...

  6. react-router 4.x 路由按需加载

    react-router 4 代码分割(按需加载) 官方文档  https://serverless-stack.com/chapters/code-splitting-in-create-react ...

  7. Linux常用命令——链接命令

    链接命令:ln ln -s [原文件] [目标文件] 命令英文原意:link 功能描述:生成链接文件 选项:-s 创建软链接,也叫符号链接 硬链接特征: 1.拥有相同的i节点和存储block块,可以看 ...

  8. Vscode下调试基于Homestead环境的Laravel框架

    PS:最近在学Laravel框架,本机IDE是Vscode,因为Vscode是真的好用!今天突然想调试php代码了,于是疯狂地在网上查资料,经过一上午的不懈努力,终于成功了! 准备工作 首先环境要保证 ...

  9. input file 美化的方法

    css input[type=file] 样式美化,input上传按钮美化 2014年8月29日 113210次浏览 由于明天公司组织出去游玩,今天把这两天的博客都写了吧,今天的内容是input[ty ...

  10. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...