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——文件及目录File操作
API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...
- Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)
控制台程序,创建和删除目录以及复制和移动文件. import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOE ...
- 【转】java 文件 读取目录下的所有文件(包括子目录)
转自:http://www.cnblogs.com/pricks/archive/2009/11/11/1601044.html import java.io.File; import java.io ...
- cp命令:复制文件和目录
cp命令:复制文件和目录 [功能说明] cp命令可以理解英文单词copy的缩写,其功能为复制文件和目录. [语法格式] 1 cp [option] [source] [dest] 2 cp [选项] ...
- 如何使用命令行编译以及运行java文件
要想编译和运行java文件,很简单,只需要两个命令: (1) javac:作用:编译java文件:使用方法: javac Hello.java ,如果不出错的话,在与Hello.java 同一目录下会 ...
- 在windows下使用cmd命令行对java文件进行编译和执行
windows下利用cmd命令行可以调用jdk里的javac.exe和java.exe对java文件进行编译和执行,前提是jdk已成功安装并正确配置相关环境变量 相关配置链接:java基础学习总结—— ...
- Eclipse中Java文件图标由实心J变成空心J的问题
在eclipse中空心J的java文件,表示不被包含在项目中进行编译,而是当做资源存在项目中.例如 当是单个文件为空心J的时候 1.右击该文件 -- >BuildPath -->Inclu ...
- Linux文件和目录操作管理命令
1.pwd:显示工作目录路径 -p:显示实际物理路径 -l:显示链接路径 2.cd:更改工作目录路径 cd:进入用户主目录 cd~:进入用户主目录 cd-:返回进入此目录之前所在的目录 cd..:返回 ...
- 第7章 Linux文件与目录管理
目录与路径 相对路径与绝对路径 目录的相关操作 . 代表此层目录 .. 代表上层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的文件夹 ~account 代表accou ...
随机推荐
- HTML5——移动端的点击、拖拽
移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...
- MyBatis 之一 简介
什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- 【译】x86程序员手册30-8.2 I/O指令
8.2 I/O Instructions I/O指令 The I/O instructions of the 80386 provide access to the processor's I/O p ...
- Zynq7000系列之芯片引脚功能综述
很多人做了很久的FPGA,知道怎么去给信号分配引脚,却对这些引脚的功能及其资源限制知之甚少:在第一章里对Zynq7000系列的系统框架进行了分析和论述,对Zynq7000系列的基本资源和概念有了大致的 ...
- c3p0参数详解
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> <property name="acquireIncrement"& ...
- h5移动端混编总结
1.通信机制:解决是否能通信的问题: 2.接口:解决调用会话问题: 3.数据.URL正确性:解决数据.URL跳转路径正确性问题.
- 基于 CentOS 搭建Seafile个人网盘
一.安装 Seafile 安装依赖环境使用 yum 安装 Python 及 MySQL: yum install python python-setuptools python-imaging pyt ...
- JavaScript中的方法
JavaScript中的方法 在JavaScript中,可以通过对象来调用对应的方法.在JavaScript中,有三个重要的window对象方法:用于显示警告信息的alert.用于显示确认信息的con ...
- TWaver GIS制作穹顶之下的雾霾地图
“我不满意,我不想等待,我也不再推诿,我要站出来做一点什么.我要做的事,就在此时,就在此刻,就在此地,就在此生”.自离职央视后,沉寂许久的知名记者.主持人柴静昨日携个人视频新作 <穹顶之下> ...
- 3D赛瓦号——整装待发!
随着岁末将至,twaver开发团队依旧马不停蹄,3d产品功能持续更新,新特效和功能目不暇接.现在,我们就利用一些新功能,制作一个全新“赛瓦号”飞船,大家看一下仿真程度是否有质的不同? 网页3d技术正在 ...