- package com.test05;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- /*
- * 思路:先对e:盘下所有文件进行遍历。
- * 如果是.java的文件,替换后缀名.java>>>.jad,
- * 将其拷贝至d:javaDir文件夹中
- *
- *
- */
- public class Demo05 {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- File dir = new File("e:\\");
- fileTolist(dir);
- }
- public static void fileTolist(File dir) throws Exception {
- // 创建要遍历的根目录
- File[] files = dir.listFiles();
- // 判断
- for (File file : files) {
- if (file.length() == 0)
- continue;
- if (file.isDirectory()) {
- fileTolist(dir);
- } else {
- if (file.getName().endsWith(".java")) {
- // 符合的文件copy到指定目录
- File destDir = new File("d:/jad");
- if (!destDir.exists())
- destDir.mkdir();
- FileInputStream fis = new FileInputStream(file);
- String fileName = file.getName().replace(".java", ".jad");
- FileOutputStream fos = new FileOutputStream(new File(
- destDir, fileName));
- copy(fis, fos);
- }
- }
- }
- }
- // copy文件
- public static void copy(InputStream ips, OutputStream ops) throws Exception {
- byte[] buf = new byte[1024];
- int num = 0;
- while ((num = ips.read(buf)) != -1) {
- ops.write(buf, 0, num);
- }
- ops.close();
- ips.close();
- }
- }
复制代码 |
|