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 ...
随机推荐
- codeforces_302D
D. Yaroslav and Time time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Django基础核心技术之Model模型的介绍与设计
Django基础核心技术之Model模型的介绍与设计原创: Yunbo Shi Python Web与Django开发 2018-05-03Django网络应用开发的5项基础核心技术包括模型(Mode ...
- Spring Boot 2.0的属性绑定
Spring Boot2.0的属性绑定 原文从Spring boot第一个版本以来,我们可以使用@ConfigurationProperties注解将属性绑定到对象.也可以指定属性的各种不同格式.比如 ...
- 新一代 Linux 文件系统 btrfs 简介
https://www.ibm.com/developerworks/cn/linux/l-cn-btrfs/ Btrfs 简介 文件系统似乎是内核中比较稳定的部分,多年来,人们一直使用 ext2/3 ...
- linux 系统 UDP 丢包问题分析思路
转自:http://cizixs.com/2018/01/13/linux-udp-packet-drop-debug?hmsr=toutiao.io&utm_medium=toutiao.i ...
- 利用gsoap库封装易用的rest框架
c++缺少web开发的框架,web框架分为异步和同步,异步的业务逻辑控制需要较强功底,同步代码实现起来容易,利于阅读理解 1.gsoap是c++写的webservice库,webservice应用层也 ...
- 经典书籍---MySQL经典书籍下载
以下是一些经典的MySQL书籍电子版,括号内为提取码,若需自取. 欢迎阅读纸质版,尊重作者版权 高性能MySQL_中文版 [ hre3 ] 高性能MySQL_英文版[ m2xj ] MySQL技术内幕 ...
- Django 缓存之配置Redis
一.cache介绍 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存. 缓存工作原理:缓存是将一些常用的数据保存内存或 ...
- Mac 当xampp里mysql无法启动的解决办法
sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start
- plotting and saving over line in paraView
probe -- provides the field values in a particular location in space To save plotoverline to csv fil ...