前幾天仔細看了看java的I/O操作,呵呵。就寫了一個操作文件的類包,功能有創建文件或目錄,刪除文件或目錄,復制文件或目錄,移動文件或目錄,設置文件或目錄屬性,查看文件或目錄大小。呵呵,功能比較簡單,源代碼為:
創建:
Java代碼
package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author wakin
*
*/
public class Create
{
/**根據字符串生成文件,如果已存在則拋出異常
*
* @param filePath
*/
public void createFile(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
//這裡很奇怪,要是不寫這兩句在windows下就看不見生成的文件。呵呵,希望大家指點一下。
FileOutputStream fos= new FileOutputStream(file);
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**根據字符串生成文件夾,如果已存在則拋出異常
*
* @param filePath
*/
public void createDir(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
file.mkdirs();
}
}
package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author wakin
*
*/
public class Create
{
/**根據字符串生成文件,如果已存在則拋出異常
*
* @param filePath
*/
public void createFile(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
//這裡很奇怪,要是不寫這兩句在windows下就看不見生成的文件。呵呵,希望大家指點一下。
FileOutputStream fos= new FileOutputStream(file);
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**根據字符串生成文件夾,如果已存在則拋出異常
*
* @param filePath
*/
public void createDir(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
file.mkdirs();
}
}
刪除:
Java代碼
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
*
* @author wakin
*
*/
public class Delete
{
/**
* 刪除指定文件。
* @param filePath
* @return
*/
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;
}
}
/**
* 遞歸刪除filePath指定的文件目錄
* @param filePath
*/
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
*/
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;
}
}
/**
* 遞歸刪除filePath指定的文件目錄
* @param filePath
*/
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
{
/**
* type為判斷是否覆蓋,1為覆蓋舊的文件,2為不覆蓋。
* return 的值為1表示覆蓋或者操作正常完成,2為不覆蓋,操作取消。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
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
* @return
* @throws IOException
*/
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()){
//遞歸 ---www.bianceng.cn
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
{
/**
* type為判斷是否覆蓋,1為覆蓋舊的文件,2為不覆蓋。
* return 的值為1表示覆蓋或者操作正常完成,2為不覆蓋,操作取消。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
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
* @return
* @throws IOException
*/
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類來完成move的操作。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
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類來完成move的操作。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
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);
}
/**
* 根據type值來設置文件的屬性 readable為true表示文件可讀,ownerOnly為true表示僅對運行本程序的用戶有效
* writable為true表示可寫,executable為true表示可執行,反之則相反。
* @param fileName
* @param type
* @return
*/
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);
}
/**
* 根據type值來設置文件的屬性 readable為true表示文件可讀,ownerOnly為true表示僅對運行本程序的用戶有效
* writable為true表示可寫,executable為true表示可執行,反之則相反。
* @param fileName
* @param type
* @return
*/
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
*/
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;
}
}
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 計算文件或目錄的空間大小。
* @author wakin
*
*/
public class Size {
/**
* 通過迭代的方法計算目錄所占空間的大小。
* @param fileName
* @return
* @throws IOException
*/
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;
}
}
呵呵,基本上就這些,可能有些問題考慮的不全面。希望大家能多提意見。