复制目录或文件以及生成临时目录或文件

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;


public class Text {


public static void main(String[] args) throws IOException {
// 复制文件
// 注释部分为把所有文件前加上时间
// 本程序中,临时文件是新建现在时间的文件夹
Date date = new Date();
SimpleDateFormat dat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-");
String ss = dat.format(date.getTime());
Scanner z = new Scanner(System.in);
System.out.println("请输入你想要复制的文件或者目录(全路径名)");
String s1 = z.next();
File f1 = new File(s1);
if (!f1.exists()) {
System.out.println("文件名有误或者文件不存在");
return;
}
System.out.println("请输入要复制到的位置(全路径名)");
String s2 = z.next();
System.out.println("临时文件是保存到新建现在时间的文件夹中");
System.out.println("是否生成临时文件?否(1)");
int s4 = z.nextInt();
if (s4 == 1) {
if (new File(s2 + s1.substring(s1.lastIndexOf("\\"))).exists()) {
if (f1.isDirectory()) {
System.out.println("是否合并目录且覆盖相同名称的文件?是(1)");
int s3 = z.nextInt();
if (s3 != 1) {
s2 = s2 + "\\" + ss;
File f = new File(s2);
f.mkdir();
}
} else {
System.out.println("是否覆盖原文件?是(1)");
int s3 = z.nextInt();
if (s3 != 1) {
s2 = s2 + "\\" + ss;
File f = new File(s2);
f.mkdir();
}
}
}
} else {
s2 = s2 + "\\" + ss;
File f = new File(s2);
f.mkdir();
}
Text f = new Text();
f.pd(f1, s2, s1.lastIndexOf("\\"));
}


public void pd(File file, String str, int a) throws IOException {
// Date date = new Date();
// SimpleDateFormat dat=new SimpleDateFormat("yyyy_MM_dd");
// String ss=dat.format(date.getTime());
// String s = file.getAbsolutePath();
// a=s.lastIndexOf("\\");
// s=s.substring(a+1);
// System.out.println(s);
// s=str+"\\"+ss+s;
String s = file.getAbsolutePath();
s = s.substring(a);
System.out.println(s);
s = str + s;
if (file.isDirectory()) {
File f = new File(s);
f.mkdir();
// String st=s;
// int d=a;
File[] c = file.listFiles();
for (File fil : c) {
// pd(fil,st,d);
pd(fil, str, a);
}
} else {
FileInputStream put = new FileInputStream(file.getAbsolutePath());
int len = put.available();
byte[] by = new byte[len];
put.read(by);
FileOutputStream out2 = new FileOutputStream(new File(s));
out2.write(by);
// put.close();
out2.close();
System.out.println(file);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值