轉自:點擊開啟連結
1,擷取檔案夾下所有檔案名稱。2,將符合要求的檔案複製出來。package com;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class FileName {/*** @param args* @throws Exception */public static void fileTest(File file, String temp) {// 是檔案的情況if (file.isFile()) {System.out.print("檔案 :" + file.getName() + "\t");}// 是目錄的情況else {File[] files = file.listFiles();for (File fileTemp : files) {if (fileTemp.isDirectory()) {System.out.println(temp + "目錄 :" + fileTemp.getName()+ "\t");fileTest(fileTemp, temp + "\t");} else {if (fileTemp.getName().length() == 11) {System.out.println(temp + "檔案 :"+ fileTemp.getName().substring(0, 5) + "\t");System.out.println(fileTemp.getName().substring(7));String fileName = fileTemp.getName().substring(0, 5)+ fileTemp.getName().substring(7);System.out.println(fileName);String c = fileTemp.getParent();File mm = new File("D:\\" + fileName); //複製檔案後要放到哪個目錄。File mm2 = new File("D:\\www\\" + fileName); //留在的檔案夾。if (fileName.equals("ABCDE.txt")) {if (fileTemp.renameTo(mm)) {System.out.println("修改成功!");FileName fs = new FileName();try {fs.CopyFile(mm, mm2);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {System.out.println("修改失敗");}}}}}}}public void CopyFile(File in, File out) throws Exception {FileInputStream fis = new FileInputStream(in);FileOutputStream fos = new FileOutputStream(out);byte[] buf = new byte[1024];int i = 0;while((i=fis.read(buf))!=-1) {fos.write(buf, 0, i);}fis.close();fos.close();}public static void main(String[] args) {// newFile中輸入檔案夾路徑 new File(path);File file = new File("D:/www"); //上來要進去的檔案夾。fileTest(file, "");}}