不多说,上代码(;;怕删帖,你懂得:D)
下面还有些地方需要修饰,思路就是这样了,,,焦虑不安啊
- /*
- *
- * 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
- *
- */
- public class Test2 {
- public static void main(String[] args) throws IOException {
- String strFile="srcfile";
- String desFile="desfile";
- copyFile(strFile,desFile);
- }
- public static void copyFile(String srcFile, String desFile)
- throws IOException {
- File src = new File(srcFile);
- File des = new File(desFile);
- if (!(src.exists())) {
- throw new RuntimeException("文件夹不存在");
- }
- if (!des.exists())
- des.mkdir();
- File[] files =src.listFiles(new FileFilter() {
- @Override
- public boolean accept(File pathname) {
- if (pathname.toString().endsWith(".java"))
- return true;
- return false;
- }
- });
- System.out.println(Arrays.toString(files));
- for (int i = 0; i < files.length; i++) {
- BufferedReader br = new BufferedReader(new FileReader(files[i]));
-
- String name = files[i].getName().replace(".java", ".txt");
- String line = null;
- File temp = new File(des,name);
- PrintWriter pw = new PrintWriter(temp);
- while ((line = br.readLine()) != null) {
- pw.println(line);
- }
- System.out.println(temp+"复制完成!");
- pw.flush();
- br.close();
- pw.close();
- }
- }
- }
复制代码
|
|