文件及目录拷贝

package eflylin;
02
import java.io.*;
03
import java.util.Date;
04
import java.text.SimpleDateFormat;
05
public class BufferedInputFile {
06

07
public static String read(String filename) throws IOException{
08
BufferedReader in = new BufferedReader(new FileReader(filename));
09
String s;
10
StringBuilder sb = new StringBuilder();
11
while((s = in.readLine())!= null){
12
sb.append(s);
13
}
14
in.close();
15
return sb.toString();
16
}
17
public static void copyfile(String inputname,String outputname) throws IOException{
18
InputStream in = new FileInputStream(inputname);
19
OutputStream out = new FileOutputStream(outputname);
20
byte[] buffer = new byte[1024];
21
int read = 0;
22
while((read = in.read(buffer))!= -1){
23
out.write(buffer,0,read);
24
System.out.print(read);
25
System.out.print(buffer);
26
System.out.print("/n");
27
}
28
in.close();
29
out.close();
30
}
31
public static void copyDir(String inputname,String outputname) throws IOException{
32
(new File(outputname)).mkdirs();
33
File[] file=(new File(inputname)).listFiles();
34
for(int i=0;i<file.length;i++){
35
if(file[i].isFile()){
36
file[i].toString();
37
FileInputStream input=new FileInputStream(file[i]);
38
//mkdir if destination does not exist
39
File outtest = new File(outputname);
40
if (!outtest.exists()){
41
outtest.mkdir();
42
}
43
FileOutputStream output=new FileOutputStream(outputname+"/"+(file[i].getName()).toString());
44
byte[] b=new byte[1024*5];
45
int len;
46
while((len=input.read(b))!=-1){
47
output.write(b,0,len);
48
}
49
output.flush();
50
output.close();
51
input.close();
52
}else if (file[i].isDirectory()){
53
//System.out.print(file[i].getAbsolutePath()+ "/n" + file[i].getName());
54
System.out.print(file[i].toString()+ "," + outputname + "//" + file[i].getName());
55
copyDir(file[i].toString(),outputname+ "//" + file[i].getName());
56
}
57
}
58
}
59
public static void main(String[] args)throws IOException{
60
//System.out.print(read("BOOTEX.LOG"));
61
//copyDir("mysrc","des2");
62
Date date = new Date();
63
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
64
System.out.println(df.format(new Date()));
65
//System.out.print(date.getMonth()+"/n");
66
//System.out.print(date.getDate()+ "/n");
67
//System.out.print(date.toGMTString()+"/n");
68
}
69
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值