- package lianxibao2;
- import java.io.*;
- /**
- * 4,编写一个程序,把这个目录下的所有的带.java文件都拷贝到另一个目录总,拷贝成功后,把后缀名是.java的改成.txt
- *
- * @author 辉辉 思路: 拷贝当前字符文件,定义字符文件读取写入流,
- */
- public class Test04 {
- public static void main(String[] args) {
- String filename1 = "c:\\test";
- String filename2 = "e:\\";
- copy(filename1, filename2);
- }
- private static void copy(String filename1, String filename2) {
- File oldFile = new File(filename1);
- String name = oldFile.getName();
- File newFile = new File(filename2 + name);
- newFile.mkdirs();
- File[] files = oldFile.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- copy(oldFile + File.separator + file.getName(), newFile + File.separator);
- } else if (file.isFile()) {
- if (file.getAbsolutePath().endsWith(".java")) {
- BufferedReader br = null;
- BufferedWriter bw = null;
- try {
- br = new BufferedReader(new FileReader(file));
- bw = new BufferedWriter(new FileWriter(newFile + File.separator
- + file.getName().replace(".java", ".txt")));
- String line = null;
- while ((line = br.readLine()) != null) {
- bw.write(line);
- bw.flush();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- }
- }
复制代码
面试时,手僵硬了 |