renameTo()还是不要用吧,剪切文件不是很好~~~这个程序我写个,可能不是很完善:
- import java.io.*;
- import java.util.*;
- /**
- * 描述查找到java文件,再修改txt文件并存到相应的目录
- */
- public class JavaToTxtDemo2 {
- public static void main(String[] args) throws IOException {
- // java文件的父路径
- String srcPath = "d:\\java";
- File parent = new File(srcPath);
- //txt文件存放的父路径
- String destPath = "D:\\useTotest\\copy\\";
- ArrayList<File> list = new ArrayList<File>();
- getJavaFiles(parent, list);
- change(list, destPath);
- }
- //parent目录下所有java文件,并存放在list中
- public static void getJavaFiles(File parent, List<File> list) {
- File[] files = parent.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- getJavaFiles(file, list);
- } else {
- if (file.getName().endsWith(".java")) {
- list.add(file);
- }
- }
- }
- }
- //遍历list中的java文件,并修为txt文件
- public static void change(List<File> list, String destPath) throws IOException {
- BufferedReader in = null;
- BufferedWriter out = null;
- for (File file : list) {
- String javaFilename = file.getName(); //java文件名
- //txt文件名
- String txtFilename = javaFilename.substring(0, javaFilename.length()-".java".length())
- + ".txt";
- in = new BufferedReader(new FileReader(file));
- out = new BufferedWriter(new FileWriter(destPath + txtFilename));
- String line = null;
- while((line = in.readLine()) != null) {
- out.write(line);
- out.newLine();
- out.flush();
- }
-
- }
- //关闭资源
- in = null;
- out = null;
- in.close();
- out.close();
- }
- }
复制代码
|