面试题2

编写一个程序,将d:java目录下的所有.java文件复制到d:jad目录下,并将原来文件的扩展名从.java改为.jad。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.maoge;

import java.io.*;

public class {
public static void main(String args[]) throws Exception {
File nativeDir = new File("D:\IDEA\workspace\file");

if (!(nativeDir.exists() && nativeDir.isDirectory())) {
throw new Exception("目录不存在");
}

File[] files = nativeDir.listFiles(
new FilenameFilter() {
public boolean accept(File file, String fileName) {
return fileName.endsWith(".java");
}
}
);

System.out.println("文件数量:" + files.length);

File distDir = new File("D:\IDEA\workspace\file\jad");
if (!(distDir.exists() || distDir.isDirectory())) {
distDir.mkdir();
}

for (File file : files) {
String distFileName = file.getName().replaceAll("\.java$", ".jad");
FileInputStream fis = new FileInputStream(file);
File f = new File(distDir, distFileName);
FileOutputStream fos = new FileOutputStream(f);
copy(fis, fos);
}

}

public static void copy(FileInputStream fis, FileOutputStream fos) throws IOException {
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
}
}

limaodeng

scribble