import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
public class TestFile{
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the Filename : ");
String fileName = sc.nextLine();
//File f = new File("d:/Test");
File f = new File(fileName);
File fAccept = new File("d:/JAVA/Receive");
if(!fAccept.mkdir()) {
System.out.println("Dir is not made!");
return;
}
// 拿到接收文件夹的Name;
String str = fAccept.getPath();
//System.out.println(f);
File [] files = f.listFiles();
FileWriter fw = null;
FileReader fr = null;
//遍历访问文件夹中的每一个文件
for(File file : files){
String s = file.getName();
// 如果拿到的文件不是以java结尾的,则跳过循环,直接访问下一个文件;
if(!s.endsWith(".java")) continue;
// 下面就是先在新目录下创建个文件,然后改个文件名,然后把文件写进去的= =。
s = s.replaceAll(".java", ".txt");
//File receive = new File(str+"/"+s);
fr = new FileReader(file.getPath());
fw = new FileWriter(str+"/"+s);
int m = 0;
while((m = fr.read())!= -1){
fw.write(m);
fw.flush();
}
fw.close();
fr.close();
}
}
} |
|