import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.filechooser.FileFilter;
public class CopyDirectory {
static int num01=0,num02=0; //num01统计所要复制的目录的文件夹个数,num02则为文件个数
static long size=0; //size统计所要复制的目录的大小
//列出系统的驱动器
public static void getDrives(){
File [] drives=new File[File.listRoots().length];
drives=File.listRoots();
char temp;
String driveName;
System.out.println("本计算机的驱动器列表如下:");
for(int i=0;i<drives.length;i++){
driveName=drives[i].getPath();
temp=driveName.charAt(0);
System.out.print(temp+"盘"+" ");
}
System.out.println();
}
//列出选定的驱动器下的目录、文件,并标出它们的属性
public static void getInfoofDrive(char drive){
if(Character.isLowerCase(drive)){
drive=Character.toUpperCase(drive);
}
File [] drives=new File[File.listRoots().length];
drives=File.listRoots();
String driveName;
boolean flag=false;//用于标示drive是否存在
int num=0;//标记是第几个盘
char [] temp=new char[drives.length];//存储各驱动器名,如C
for(int i=0;i<drives.length;i++){
driveName=drives[i].getPath();
temp[i]=driveName.charAt(0);
}
for(int j=0;j<temp.length;j++){
if(drive==temp[j]){
flag=true;
num=j;
break;
}
}
if(flag){
File [] temp02=drives[num].listFiles();
SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date temp03;
int count01=0,count02=0;
System.out.println(temp[num]+"盘下的文件目录信息如下:");
System.out.println("最后修改时间 属性 名称");
for(int k=0;k<temp02.length;k++){
temp03=new Date(temp02[k].lastModified());
System.out.print(fmt.format(temp03)+" ");
if(temp02[k].isDirectory()){
count01++;
System.out.print("文件夹"+" ");
System.out.println(temp02[k].getName());
}
else {
count02++;
System.out.print("文件 "+" ");
System.out.println(temp02[k].getName()+"(大小:"+temp02[k].length()+" Byte)");
}
}
System.out.println("共"+count01+"个文件夹,"+count02+"个文件.");
}
else {
System.out.println("你所输入的驱动器不存在!");
}
}
//列出选定路径的文件或文件夹的属性(只列出其下一级的子目录)
//参数path的格式为 D:\\Java ,在主程序中给予说明
public static void getInfoofPath(String path){
File temp01=new File(path);
if(temp01.exists()){
File [] info=temp01.listFiles();
SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date temp02;
int count01=0,count02=0;
System.out.println(temp01.getAbsolutePath()+" 的目录如信息下:");
System.out.println("最后修改时间 属性 名称");
if(temp01.isFile()){
temp02=new Date(temp01.lastModified());
System.out.print(fmt.format(temp02)+" ");
System.out.print("文件 ");
System.out.println(temp01.getName()+"(大小:"+temp01.length()+" Byte)");
System.out.println("共1个文件.");
}
else {
for(int i=0;i<info.length;i++){
temp02=new Date(info[i].lastModified());
System.out.print(fmt.format(temp02)+" ");
if(info[i].isDirectory()){
System.out.print("文件夹 ");
System.out.println(info[i].getName());
count01++;
}
else {
System.out.print("文件 ");
System.out.println(info[i].getName()+"(大小:"+info[i].length()+" Byte)");
count02++;
}
}
System.out.println("共"+count01+"文件夹,"+count02+"文件.");
}
}
else {
System.out.println("文件或文件夹不存在!");
}
}
//列出所要复制的文件或文件夹的详细信息(无论它的子目录有几个层次,均一一列出)
//参数的输入格式如 D:\\java
//当要调用到此方法时,先写上"System.out.println("最后修改时间 属性 名称");"
public static void getInfoofCopyFile(String path){
File temp01=new File(path);
if(temp01.exists()){
SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date temp02;
if(temp01.isDirectory()){
File [] info=temp01.listFiles();
for(int i=0;i<info.length;i++){
temp02=new Date(info[i].lastModified());
System.out.print(fmt.format(temp02)+" ");
if(info[i].isDirectory()){
System.out.print("文件夹 ");
System.out.println(info[i].toString());
num01++;
getInfoofCopyFile(info[i].toString());
}
else {
System.out.print("文件 ");
System.out.println(info[i].toString()+"(大小:"+info[i].length()+" Byte)");
num02++;
size+=info[i].length();
}
}
}
else {
temp02=new Date(temp01.lastModified());
System.out.print(fmt.format(temp02)+" ");
System.out.print("文件 ");
System.out.println(temp01.toString()+"(大小:"+temp01.length()+" Byte)");
num02++;
size+=temp01.length();
}
}
else {
System.out.println("文件或文件夹不存在!");
}
}
//由于num01、num02和size定义为静态常量,故当要反复使用时,为避免累加,则须清零
public static void clearNum(){
num01=num02=0;
size=0;
}
//复制文件:当前目录若是文件夹,则创建一个新的文件夹;若为文件,则创建一个新的同类型的文件,并把其中的内容写入到新文件中
//写文件采用通道方法,效率较高
//按题目要求,参数fromPath和toPath所指定的目录要么同是文件,要么同是文件夹,否则将出错
public static void copyFiles(String fromPath,String toPath) throws Exception{
File fromFile=new File(fromPath);
if(fromFile.exists()){
File toFile=new File(toPath);
if(toFile.exists()){
System.out.println("目录"+toPath+"已经存在,复制文件操作失败!");
}//end of if
else {
if(fromFile.isFile()){
//复制文件
File newToFile=new File(toPath);
newToFile.createNewFile();
FileInputStream inFile=new FileInputStream(fromFile);
FileOutputStream outFile=new FileOutputStream(newToFile);
FileChannel inChannel=inFile.getChannel();
FileChannel outChannel=outFile.getChannel();
long bytesWritten=0;
long byteCount=inChannel.size();
while(bytesWritten<byteCount){
bytesWritten+=inChannel.transferTo(bytesWritten, byteCount-bytesWritten, outChannel);
}
System.out.println("文件"+fromFile.getName()+"已经成功复制到"+newToFile.getAbsolutePath()+".");
inFile.close();
outFile.close();
}//end of if
else {
//处理文件夹
if(toFile.mkdir()){
//复制文件夹
System.out.println("目录"+toFile.getAbsolutePath()+"已经创建!");
File [] info=fromFile.listFiles();
for(int i=0;i<info.length;i++){
String toPathTemp=toPath+"\\"+info[i].getName();
copyFiles(info[i].getAbsolutePath(),toPathTemp);//递归调用
}
}//end of if
else {
System.out.println("目录"+toFile.getAbsolutePath()+"创建失败!");
}//end of else
}//end of else
}//end of else
}//end of if
else {
System.out.println("目录"+fromPath+"不存在,复制文件操作失败!");
}//end of else
}
/**
* @param args
*/
public static void main(String[] args) throws InterruptedException,IOException,Exception{
// TODO 自动生成方法存根
System.out.println("Java作业3:将指定的目录复制到另一指定的目录");
System.out.println("作者:05计科07班 林锭钦 3105007084");
System.out.println();
System.out.println("欢迎使用本系统!");
System.out.println("本系统两大功能:文件信息查询和文件复制功能");
System.out.println("使用须注:目录输入路径的格式如--C:\\\\java (两个斜杠)");
System.out.println();
while(true){
BufferedReader input=new BufferedReader(new InputStreamReader(Sys
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
复制目录.rar (2个子文件)
复制目录
CopyDirectory.java 11KB
CopyDirectory.class 7KB
共 2 条
- 1
资源评论
dingqinlin
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功