您的位置:首页 > 编程语言 > Java开发

java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作

2010-03-05 09:55 966 查看
已经3月份了,虽说兰州3月天气变化无常,但毕竟春天已经走来,到处散发着沁人的春天的泥土气息,勃勃的生机给人以朝气,荡漾的绿意就在眼前,!

最近写了一个关于文件操作的类,给大家分享一下,如果您发现了问题,或者也有更好的改进,也希望拿出来分享一下,可以给我留言,谢谢啦 :)

]package util;

import java.io.*;

/**
* 提供对文件、文件夹 的建立、删除、复制以及移动等功能
* 文件操作工具类
* @author 金涛
* @version 1.0.0
* @date2010-3-4
*/
public class FileUtil {

/**
* 新建目录并返回该新建的目录
* @param folderPath String 文件夹路径
* @return File 新建的目录
*/
public File newFolder (String folderPath) throws Exception {
try {
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdir();
}
return folder;
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("新建目录操作出错");
}
}

/**
* 新建目录 如果目录路径中有不存在的目录则也进行建立 并返回该新建的目录
* @param folderPath String 目录路径
* @return File 新建的目录
*/
public File newFolderAndPath (String folderPath) throws Exception {
try {
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
return folder;
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("新建目录操作出错");
}
}

/**
* 新建文件,从数据如输入流获得数据
* @param filePath String 要新建文件路径及名称
* @param fis
* @return file File 返回建立的文件
*/
public File newFile(String filePath, FileInputStream fis) throws Exception {
int len = 0;
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fs = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
fs.write(buffer, 0, len);
}
fs.flush();
fs.close();
fis.close();
return file;
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("新建一个文件出错");
}
}

/**
* 新建一个字符文件,并向其中添加内容
* @param filePath 新建的带文件名的文件路径
* @param conttent 文件内容
* @return file File 返回建立的文件
* @throws Exception
*/
public File newFile(String filePath,String conttent) throws Exception {
try{
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter resultFile = new FileWriter(filePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = conttent;
myFile.println(strContent);
myFile.close();
resultFile.close();
return file;
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("新建一个字符文件出错");
}
}

/**
* 从文件中获得字节数组
* @param f File文件
* @return
*/
private byte[] getBytesFromFile(File f) throws Exception {
int len = 0;
if (f == null && !f.exists()) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1024];
while ((len = stream.read(b)) != -1)
out.write(b, 0, len);
stream.close();
out.close();
return out.toByteArray();
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("从文件中获得字节数组出错");
}
}

/**
* 不抛出异常的文件删除
* @param file java.io.File 需要删除的文件对象
* @return boolean true = 文件删除成功, false = 文件删除失败
*/
public boolean delFileOnNoE(File file){
try{
return file.delete();
}catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 可以抛出异常的文件删除
* @param file java.io.File 需要删除的文件对象
* @return boolean true = 文件删除成功, false = 文件删除失败
*/
public boolean delFileOnE(File file) throws Exception{
try{
if (file.delete()) {
return true;
}else {
throw new Exception("不能删除文件"+file.getPath()+File.separator+file.getName());
}
}catch (Exception e) {
e.printStackTrace();
throw new Exception("删除文件异常"+e.getMessage());
}
}

/**
* 根据路径删除文件
* @param filePathAndName String 文件路径及名称
* @return boolean (true= 删除成功,false= 删除失败)
*/
public boolean delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
java.io.File myDelFile = new java.io.File(filePath);
if (!myDelFile.delete()) {
throw new Exception ("删除文件失败");
}
return true;
}catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
return false;
}
}

/**
* 根据路径删除文件夹
* @param path String 文件夹路径
* @param isThrowException boolean (true = 删除文件出错时抛出异常,false = 删除文件出错时不抛出异常,继续执行)
* @param isDel boolean (true= 删除整个文件夹, false=只删除文件夹里的所有内容)
* @return long (当isThrowException为false时:0 = 删除文件夹成功,>0 表示删除文件夹下边的文件时,出错的个数
*               当isThrowException为true时:0 = 删除文件夹成功,当删除出错时会抛出异常)
* @exception
*/
private long delFileOp(String path,boolean isDel,boolean isThrowException)throws Exception {
long flag = 0l;
try {
File dir = new File(path);//获得该文件夹
if (dir.exists() && dir.isDirectory()) {
File[] dirAndFiles = dir.listFiles();//返回此抽象路径中的文件和目录
for (int i=0; i<dirAndFiles.length; i++) {
if (dirAndFiles[i].isDirectory()) {
flag += delFileOp(dirAndFiles[i].getAbsolutePath(),true,isThrowException);//要删除子文件夹里的所有内容
}else {
if (isThrowException) {
if (!this.delFileOnE(dirAndFiles[i])) {
flag++;
}
}else{
if (!this.delFileOnNoE(dirAndFiles[i])) {
flag++;
}
}
}
}
if (dir.listFiles().length==0 && isDel) {//删除自身是空的文件夹
if (isThrowException) {
if (!this.delFileOnE(dir)) {
flag++;
}
}else{
if (!this.delFileOnNoE(dir)) {
flag++;
}
}
}
}
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("删除文件夹出错"+e.getMessage());
}
return flag;
}

/**
* 根据路径删除文件夹里的内容
* @param folderPath String 文件夹路径
* @param isThrowException boolean (true = 删除文件出错时抛出异常,false = 删除文件出错时不抛出异常,继续执行)
* @return long (当isThrowException为false时:0 = 删除文件夹成功,>0 表示删除文件夹下边的文件时,出错的个数
*               当isThrowException为true时: 0 = 删除文件夹成功,当删除出错时会抛出异常)
* @throws Exception
*/
public long delFolderContent(String folderPath,boolean isThrowException) throws Exception {
try {
return delFileOp(folderPath,false,isThrowException); //删除完里面所有内容
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("删除文件夹内容出错"+e.getMessage());
}
}

/**
* 根据路径删除文件夹
* @param filePathAndName String 文件夹路径及名称
* @param isThrowException boolean (true = 删除文件出错时抛出异常,false = 删除文件出错时不抛出异常,继续执行)
* @return long (当isThrowException为false时:0 = 删除文件夹成功,>0 表示删除文件夹下边的文件时,出错的个数
*               当isThrowException为true时:0 = 删除文件夹成功,当删除出错时会抛出异常)
* @exception
*/
public long delFolder(String folderPath,boolean isThrowException) throws Exception {
try {
return delFileOp(folderPath,true,isThrowException); //删除完里面所有内容
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("删除文件夹出错"+e.getMessage());
}
}

/**
* 复制单个文件
* @param oldPath String 原文件路径 得包括文件名
* @param newPath String 目标路径 得包括文件名
*/
public void copyFile(String oldPath, String newPath) throws Exception{
int b = 0;
try {
File oldFile = new File(oldPath);
if (oldFile.exists()) {//文件存在时
InputStream ins = new FileInputStream(oldPath);
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ((b = ins.read(buffer)) != -1) {//一次读入1024个字节 如果效率低可以重新改变缓冲区大小
fs.write(buffer, 0, b);
}
fs.flush();
fs.close();
ins.close();
}
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("复制单个文件操作出错");
}
}

/**
* 复制文件夹到另一个目录
* @param copyPath String 原文件夹路径
* @param targetPath String 目标文件夹路径
* @param isCopy boolean true=复制文件夹到另一个目录,false=只复制文件夹下面的文件到另一个目录
* @return boolean
*/
private void copyFolderOp(String copyPath, String targetPath,boolean isCopy) throws Exception {
FileInputStream ins = null;
FileOutputStream os = null;
int len = 0;
try {
File copyFolder = new File(copyPath);
File targetFolder = new File(targetPath);
File newFolder = null;
if(!targetFolder.exists() && !targetFolder.mkdirs()) {//如果目标路径不存在,则建立目标路径
throw new Exception ("文件夹:"+targetPath+"建立失败");
}
if (copyFolder.exists() && copyFolder.isDirectory()) {
if (isCopy) {
newFolder = this.newFolderAndPath(targetPath +File.separator+ copyFolder.getName());//在目标文件夹内建立原文件夹
}else {
newFolder = targetFolder;
}
File [] oldFiles = copyFolder.listFiles();
for (int i=0; i <oldFiles.length; i++) {
if(oldFiles[i].isFile()){
this.copyFile(oldFiles[i].getPath(),newFolder.getPath()+ File.separator +oldFiles[i].getName());
}else {//如果是子文件夹
copyFolderOp(oldFiles[i].getPath(),newFolder.getPath(),true);
}
}
}
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("复制整个文件夹出错");
}
}

/**
* 复制文件夹里的内容到指定目录
* @param copyPath String 原文件夹路径
* @param targetPath String 目标文件夹路径
* @throws Exception
*/
public void copyFolderContent(String copyPath, String targetPath) throws Exception {
try{
copyFolderOp(copyPath,targetPath,false);
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("复制文件夹"+copyPath+"里的内容,到"+targetPath+"目录出错");
}
}

/**
* 复制整个文件夹到指定目录
* @param copyPath
* @param targetPath
* @throws Exception
*/
public void copyAllFolder(String copyPath, String targetPath) throws Exception {
try{
copyFolderOp(copyPath,targetPath,true);
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("复制整个文件夹"+copyPath+",到"+targetPath+"目录出错");
}
}

/**
* 移动文件到指定目录
* @param oldPath String
* @param newPath String
*/
public void moveFile(String oldPath, String newPath) throws Exception {
try{
copyFile(oldPath, newPath);
delFile(oldPath);
}catch (Exception e) {
e.printStackTrace();
throw new Exception ("移动文件"+oldPath+"到"+newPath+"出错");
}
}

/**
* 移动文件夹到指定目录
* @param oldPath String
* @param newPath String
*/
public void moveFolder(String oldPath, String newPath) throws Exception {
try{
copyFolderOp(oldPath, newPath,true);
delFolder(oldPath,false);
}catch(Exception e) {
e.printStackTrace();
throw new Exception ("移动文件夹"+oldPath+"到"+newPath+"出错");
}
}

/**
* @param args
*/

public static void main(String[] args){
FileUtil fu = new FileUtil();
try{
fu.newFile("E://test//test.html", "<html><head><title></title></head><body><a href="http://www.sina.com" mce_href="http://www.sina.com" >测试</a></body></html>");
fu.copyAllFolder("E://test", "E://tmp");
fu.delFolder("E://tmp//test",true);
}catch(Exception e) {
e.printStackTrace();
}
}

}


2010-03-05 10:05:46
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: