用java写的一个文件操作类包

用java写的一个文件操作类包
关键字: 转

前几天仔细看了看java的I/O操作,呵呵。就写了一个操作文件的类包,功能有创建文件或目录,删除文件或目录,复制文件或目录,移动文件或目录,设置文件或目录属性,查看文件或目录大小。呵呵,功能比较简单,源代码为:
创建:
Java代码
package fileOperation;

import java.io.File;

/**创建一个新的文件或目录
* @author wakin
*
*/
public class Create
{
/**
* 根据路径名创建文件
* @param filePath 路径名
* @return 当创建文件成功后,返回true,否则返回false.
*
*/
public boolean createFile(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
isDone = file.createNewFile();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return isDone;

}

/**
* 根据路径名创建目录
* @param filePath 路径名
* @return 当创建目录成功后,返回true,否则返回false.
*/
public boolean createDir(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("FileDirectory: "+filePath+" is already exist");
isDone = file.mkdirs();
return isDone;
}

}

package fileOperation;

import java.io.File;

/**创建一个新的文件或目录
* @author wakin
*
*/
public class Create
{
/**
* 根据路径名创建文件
* @param filePath 路径名
* @return 当创建文件成功后,返回true,否则返回false.
*
*/
public boolean createFile(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
isDone = file.createNewFile();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return isDone;

}

/**
* 根据路径名创建目录
* @param filePath 路径名
* @return 当创建目录成功后,返回true,否则返回false.
*/
public boolean createDir(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("FileDirectory: "+filePath+" is already exist");
isDone = file.mkdirs();
return isDone;
}

}


删除:
Java代码
package fileOperation;

import java.io.File;
import java.io.IOException;

/**
* 删除一个文件或目录
* @author wakin
*
*/
public class Delete
{

/**
* 删除路径名所代表的文件。
* @param filePath 路径名
* @return 当文件成功删除,返回true,否则返回false.
*/
public boolean deleteFile(String filePath) throws IOException{
File file = new File(filePath);
if(file.exists()) {
file.delete();
//System.out.println(filePath+"文件已删除.");
return true;
}
else {
//System.out.println("逻辑错误:"+filePath+"文件不存在.");
return false;
}
}

/**
* 删除路径名所代表的目录
* @param filePath 路径名
* @return 当目录成功删除,返回true,否则返回false.
*/
public boolean deleteDir(String filePath) throws IOException{
boolean isDone = false;
File file = new File(filePath);
//判断是文件还是目录
if(file.exists()&&file.isDirectory()){
if(file.listFiles().length==0){
file.delete();
isDone = true;
}
else {
File [] delFiles = file.listFiles();
for(int i=0;i<delFiles.length;i++){
if(delFiles[i].isDirectory()){
deleteDir(delFiles[i].getAbsolutePath()); //递归调用deleteDir函数
}
else {
delFiles[i].delete();
}
}
}
//删除最后剩下的目录名。
deleteDir(filePath);
isDone = true;
}
else
return false;
return isDone;
}

}

package fileOperation;

import java.io.File;
import java.io.IOException;

/**
* 删除一个文件或目录
* @author wakin
*
*/
public class Delete
{

/**
* 删除路径名所代表的文件。
* @param filePath 路径名
* @return 当文件成功删除,返回true,否则返回false.
*/
public boolean deleteFile(String filePath) throws IOException{
File file = new File(filePath);
if(file.exists()) {
file.delete();
//System.out.println(filePath+"文件已删除.");
return true;
}
else {
//System.out.println("逻辑错误:"+filePath+"文件不存在.");
return false;
}
}

/**
* 删除路径名所代表的目录
* @param filePath 路径名
* @return 当目录成功删除,返回true,否则返回false.
*/
public boolean deleteDir(String filePath) throws IOException{
boolean isDone = false;
File file = new File(filePath);
//判断是文件还是目录
if(file.exists()&&file.isDirectory()){
if(file.listFiles().length==0){
file.delete();
isDone = true;
}
else {
File [] delFiles = file.listFiles();
for(int i=0;i<delFiles.length;i++){
if(delFiles[i].isDirectory()){
deleteDir(delFiles[i].getAbsolutePath()); //递归调用deleteDir函数
}
else {
delFiles[i].delete();
}
}
}
//删除最后剩下的目录名。
deleteDir(filePath);
isDone = true;
}
else
return false;
return isDone;
}

}


复制:
Java代码
package fileOperation;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 复制文件和文件夹工具类,能判断源文件不存在,源文件不可读,目标文件已经存在,
* 目标路径不存在,目标路径不可写等情况
* @author wakin
*
*/
public class Copy
{

/**根据源路径名和目标路径名复制文件。
*
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的文件,2为不覆盖,操作取消。
* @return 当复制成功时返回1, 当目标文件存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int copyFile(
String source_name,
String dest_name,
int type) throws IOException {
int result = 0;
int byte_read;
byte [] buffer;
File source_file = new File(source_name);
File dest_file = new File(dest_name);
FileInputStream source = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileOutputStream dest = null;
try {
if(!source_file.exists() || !source_file.isFile()) //不存在
throw new RuntimeException("FileCopy: no such source file:"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("FileCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(dest_file.isFile()) {
if(type==1) {
dest_file.delete(); //覆盖
result = 1 ;
}
else if(type==2) {
result = 2;
return result; //不覆盖 ,程序返回。
}
}
else
throw new RuntimeException("FileCopy: destination"+dest_name
+"is not a file.");
}
else {
File parentDir = new File(dest_file.getParent()); //获得目录信息
if(!parentDir.exists()) {
throw new RuntimeException("FileCopy: destination"+dest_name
+"directory doesn't exist."); //目录不存在
}
if(!parentDir.canWrite())
throw new RuntimeException("FileCopy: destination"+dest_name
+"is unwriteable."); //目录不可写
}

//开始复制文件
//输入流
source = new FileInputStream(source_file);
bis = new BufferedInputStream(source);
//输出流
dest = new FileOutputStream(dest_file);
bos = new BufferedOutputStream(dest);
buffer = new byte[1024*5];
while((byte_read=bis.read(buffer))!=-1) {
bos.write(buffer, 0, byte_read);
}
result = 1;
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(source!=null){
bis.close();
source.close();
}
if(dest!=null) {
bos.flush();
bos.close();
dest.flush();
dest.close();
}
}
return result;

}

/**根据源路径名和目标路径名复制目录。
* 复制目录以复制文件为基础,通过递归复制子目录实现。
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的目录,2为不覆盖,操作取消。
* @return 当复制成功时返回1, 当目标目录存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int copyDirectory(
String source_name,
String dest_name,
int type
) throws IOException {
File source_file = new File(source_name);
File dest_file = new File(dest_name);
int result = 0;
Delete del = new Delete(); //用于删除目录文件
if(!source_file.exists()||source_file.isFile()) //不存在
throw new RuntimeException("DirCopy: no such dir"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("DirCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(type==1) {
del.deleteDir(dest_name); //覆盖
result = 1;
}
if(type==2) {
result = 2; //不覆盖
return result;
}
}
if(!dest_file.exists()) {
new File(dest_name).mkdirs(); //创建目标目录
File[] fileList = source_file.listFiles();
for(int i=0;i<fileList.length;i++){
System.out.println(fileList[i].getName());
if(fileList[i].isFile()){
//用copyFile函数复制文件
this.copyFile(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(),
type);
}
else if(fileList[i].isDirectory()){
//递归
copyDirectory(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(), type);
}
}
result = 1;

}

return result;
}
}

package fileOperation;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 复制文件和文件夹工具类,能判断源文件不存在,源文件不可读,目标文件已经存在,
* 目标路径不存在,目标路径不可写等情况
* @author wakin
*
*/
public class Copy
{

/**根据源路径名和目标路径名复制文件。
*
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的文件,2为不覆盖,操作取消。
* @return 当复制成功时返回1, 当目标文件存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int copyFile(
String source_name,
String dest_name,
int type) throws IOException {
int result = 0;
int byte_read;
byte [] buffer;
File source_file = new File(source_name);
File dest_file = new File(dest_name);
FileInputStream source = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileOutputStream dest = null;
try {
if(!source_file.exists() || !source_file.isFile()) //不存在
throw new RuntimeException("FileCopy: no such source file:"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("FileCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(dest_file.isFile()) {
if(type==1) {
dest_file.delete(); //覆盖
result = 1 ;
}
else if(type==2) {
result = 2;
return result; //不覆盖 ,程序返回。
}
}
else
throw new RuntimeException("FileCopy: destination"+dest_name
+"is not a file.");
}
else {
File parentDir = new File(dest_file.getParent()); //获得目录信息
if(!parentDir.exists()) {
throw new RuntimeException("FileCopy: destination"+dest_name
+"directory doesn't exist."); //目录不存在
}
if(!parentDir.canWrite())
throw new RuntimeException("FileCopy: destination"+dest_name
+"is unwriteable."); //目录不可写
}

//开始复制文件
//输入流
source = new FileInputStream(source_file);
bis = new BufferedInputStream(source);
//输出流
dest = new FileOutputStream(dest_file);
bos = new BufferedOutputStream(dest);
buffer = new byte[1024*5];
while((byte_read=bis.read(buffer))!=-1) {
bos.write(buffer, 0, byte_read);
}
result = 1;
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(source!=null){
bis.close();
source.close();
}
if(dest!=null) {
bos.flush();
bos.close();
dest.flush();
dest.close();
}
}
return result;

}

/**根据源路径名和目标路径名复制目录。
* 复制目录以复制文件为基础,通过递归复制子目录实现。
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的目录,2为不覆盖,操作取消。
* @return 当复制成功时返回1, 当目标目录存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int copyDirectory(
String source_name,
String dest_name,
int type
) throws IOException {
File source_file = new File(source_name);
File dest_file = new File(dest_name);
int result = 0;
Delete del = new Delete(); //用于删除目录文件
if(!source_file.exists()||source_file.isFile()) //不存在
throw new RuntimeException("DirCopy: no such dir"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("DirCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(type==1) {
del.deleteDir(dest_name); //覆盖
result = 1;
}
if(type==2) {
result = 2; //不覆盖
return result;
}
}
if(!dest_file.exists()) {
new File(dest_name).mkdirs(); //创建目标目录
File[] fileList = source_file.listFiles();
for(int i=0;i<fileList.length;i++){
System.out.println(fileList[i].getName());
if(fileList[i].isFile()){
//用copyFile函数复制文件
this.copyFile(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(),
type);
}
else if(fileList[i].isDirectory()){
//递归
copyDirectory(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(), type);
}
}
result = 1;

}

return result;
}
}


移动:本来想用renameTo方法来实现的,但是发现这个方法有些问题。在我的博客里写明了,希望大家能指点一下。
Java代码
package fileOperation;

import java.io.File;
import java.io.IOException;
/**
* 移动文件/目录,利用Delete类和Copy类来实现。
* @author wakin
*
*/
public class Move {
/**
* 利用Copy类的函数和Delete类来完成移动文件/目录的操作。
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的文件/目录,2为不覆盖操作取消。
* @return 当移动成功后返回1,当目标目录存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int move(String source_name,String dest_name,int type) throws IOException{
int result = 0;
Copy copy = new Copy();
Delete delete = new Delete();
File source_file = new File(source_name);
//File dest_file = new File(dest_name);
if(!source_file.exists())
throw new RuntimeException("FileMove: no such source file:"+source_name);
if(source_file.isFile()){
result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数
if(result ==1)
delete.deleteFile(source_name); //调用Delete类的deleteFile函数删除源文件
}
else {
result = copy.copyDirectory(source_name, dest_name, type);//调用Copy类的copyDirectory函数
if(result == 1)
delete.deleteDir(source_name); //调用Delete类的deleteDir函数删除源目录
}
return result;
}
}

package fileOperation;

import java.io.File;
import java.io.IOException;
/**
* 移动文件/目录,利用Delete类和Copy类来实现。
* @author wakin
*
*/
public class Move {
/**
* 利用Copy类的函数和Delete类来完成移动文件/目录的操作。
* @param source_name 源路径名
* @param dest_name 目标路径名
* @param type type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的文件/目录,2为不覆盖操作取消。
* @return 当移动成功后返回1,当目标目录存在且type值为2时返回2,其他情况返回0
* @throws IOException 发生I/O错误
*/
public int move(String source_name,String dest_name,int type) throws IOException{
int result = 0;
Copy copy = new Copy();
Delete delete = new Delete();
File source_file = new File(source_name);
//File dest_file = new File(dest_name);
if(!source_file.exists())
throw new RuntimeException("FileMove: no such source file:"+source_name);
if(source_file.isFile()){
result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数
if(result ==1)
delete.deleteFile(source_name); //调用Delete类的deleteFile函数删除源文件
}
else {
result = copy.copyDirectory(source_name, dest_name, type);//调用Copy类的copyDirectory函数
if(result == 1)
delete.deleteDir(source_name); //调用Delete类的deleteDir函数删除源目录
}
return result;
}
}


查看,设置属性:
Java代码
package fileOperation;

import java.io.File;
/**
* 查看,修改文件或目录的属性
* @author wakin
*
*/
public class Attribute {
/**
* 查看路径名所表示文件或目录的属性。
* @param fileName 路径名
*/
public void lookAttribute(String fileName) {
boolean canRead;
boolean canWrite;
boolean canExecute;
File file = new File(fileName);
if(!file.exists())
throw new RuntimeException("File:"+fileName+"is not exist");
canRead = file.canRead();
canWrite = file.canWrite();
canExecute = file.canExecute();
System.out.println("Can read:"+canRead+" Can write:"+canWrite+" Can Execute:"+canExecute);
}
/**
* 设置路径名所表示的文件或目录的的属性。?部分功能可能在windows下无效。
* @param fileName 路径名
* @param readable 是否可读
* @param writable 是否可写
* @param executable 是否可执行
* @param ownerOnly 是否用户独享
* @return 属性设置成功,返回true,否则返回false
*/
public boolean setAttribute(
String fileName,
boolean readable,
boolean writable,
boolean executable,
boolean ownerOnly)
{
boolean isDone = false;
File file = new File(fileName);
isDone = file.setReadable(readable, ownerOnly)
&& file.setWritable(writable, ownerOnly)
&& file.setExecutable(executable, ownerOnly);
return isDone;
}
}

package fileOperation;

import java.io.File;
/**
* 查看,修改文件或目录的属性
* @author wakin
*
*/
public class Attribute {
/**
* 查看路径名所表示文件或目录的属性。
* @param fileName 路径名
*/
public void lookAttribute(String fileName) {
boolean canRead;
boolean canWrite;
boolean canExecute;
File file = new File(fileName);
if(!file.exists())
throw new RuntimeException("File:"+fileName+"is not exist");
canRead = file.canRead();
canWrite = file.canWrite();
canExecute = file.canExecute();
System.out.println("Can read:"+canRead+" Can write:"+canWrite+" Can Execute:"+canExecute);
}
/**
* 设置路径名所表示的文件或目录的的属性。?部分功能可能在windows下无效。
* @param fileName 路径名
* @param readable 是否可读
* @param writable 是否可写
* @param executable 是否可执行
* @param ownerOnly 是否用户独享
* @return 属性设置成功,返回true,否则返回false
*/
public boolean setAttribute(
String fileName,
boolean readable,
boolean writable,
boolean executable,
boolean ownerOnly)
{
boolean isDone = false;
File file = new File(fileName);
isDone = file.setReadable(readable, ownerOnly)
&& file.setWritable(writable, ownerOnly)
&& file.setExecutable(executable, ownerOnly);
return isDone;
}
}


查看大小:
Java代码
package fileOperation;

import java.io.File;
import java.io.IOException;
/**
* 计算文件或目录的空间大小。
* @author wakin
*
*/
public class Size {
/**
* 计算路径名代表的文件或目录所占空间的大小。
* @param fileName 路径名
* @return 所占字节数
* @throws IOException 发生I/O错误
*/
public static long getSize(String fileName) throws IOException {
long result = 0;
File file = new File(fileName);
if(!file.exists())
throw new RuntimeException("No such source file:"+fileName);
if(file.isFile()){
return file.length();
}
else {
String [] FileList = file.list();
for(int i =0;i<FileList.length;i++) {
result += getSize(fileName+"/"+FileList[i]); //迭代
}
}
return result;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值