您的位置:jsp学习站首页 >> JAVA类 >> JAVA高级 >> 一个用Java实现了文件基本管理的软件

一个用Java实现了文件基本管理的软件 (1)

[ 来源:互网络 | 更新日期:2007-09-06 06:54:07 | 浏览次数:5176]
简介:angel
功能如题,详细信息请看代码中的注释,如果有不明白的地方可以到本站的Java版提问,我回尽快给出答复。
该软件主要实现了六个功能,拷贝、删除、根据指定的路径查找文件,根据指定的时间查找文件、根据参数建立目录和实现文件的移动。

package com.angel.filemanage;

import java.io.*;
import java.lang.Exception;
import java.util.Vector;


/**
* 本文件归属man_angel
* 本文件用于文件的拷贝
* 开发人员:angel
* 开发日期:2002-03-10
*/

/**********************************************
功能说明:
★ 实现文件的拷贝
***********************************************/
public class copyFile
{

String sourcePath;
String destinationPath;
int buffer;
long fileLength;
FileInputStream sourceFile;
FileOutputStream destinationFile;
boolean makeDirs;
boolean isMoving;
boolean fieldSss;

/***********************************************
功能: 拷贝文件
使用说明:
参数含义: 无
返回值含义:-1:源文件不存在
-2:源文件是目录
-3:给定的目标路径不存在且不能创建路径
-4:拷贝时出错
1:拷贝成功
************************************************/
protected int computeFunction()
{
File sourceFile = new File(sourcePath);
File destFile = new File(destinationPath);
File destinationFile=null;
FileInputStream sourceFileInputStream = null;
FileOutputStream destinationFileOutputStream = null;

byte buf[] = new byte[buffer];
int counter = 0;
if(!sourceFile.exists())
{
return -1;

}
if(sourceFile.isDirectory()) //如果源文件是目录,抛出异常。
{
return -2;
}

try
{
if(destFile.isDirectory()){
String srcName=sourceFile.getName();
if(destinationPath.endsWith("\")==false)
destinationPath+="\";
destinationPath+=srcName;
}

destinationFile=new File(destinationPath);
String parentDirectory = destinationFile.getParent();
File parentDirFile = new File(parentDirectory);
if(!parentDirFile.exists())
{
if(!makeDirs) //如果目标文件的目录不存在,又不能创建新目录,抛出异常。
{
return -3;
}
if(makeDirs) //目标文件的目录不存在,则创建新目录。
parentDirFile.mkdirs(); //创建名为parentDirFile的目录。
}
}catch(NullPointerException _ex) { }
long oldFileLength = fileLength;
fileLength = sourceFile.length();

try
{
sourceFileInputStream = new FileInputStream(sourceFile);
destinationFileOutputStream = new FileOutputStream(destinationFile);
while((counter = sourceFileInputStream.read(buf)) != -1) //从输入流中读buf长度的数据。
{
destinationFileOutputStream.write(buf, 0, counter); //从buf中往输出流中写入counter长度的数据。


}
}
catch(IOException e)
{
System.out.println("拷贝文件时出错:"+e.toString());
return -4;
}
if(destinationFileOutputStream != null)
try
{
destinationFileOutputStream.close(); //关闭输出流。
}catch(IOException e)
{
System.out.println("关闭目标文件时出错:"+e.toString());
return -4;
}
try
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Tags:关键字:一个用Java实现了文件基本管理的软件
责任编辑:glen