- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.FilenameFilter;
- import java.io.IOException;
- public class Notes {
- private static String s2 = "G:\\黑马视频\\io笔记2.txt";
- public static void main(String[] args) {
- String dirpath = "F:\\workspace\\day28\\src\\IObij\\a";
- listDe(dirpath);
-
- }
- public static void Copy(String s1,String s2){
- FileWriter fw = null;
- FileReader fr = null;
- try{
- String fuhao = "\r\n=================================================================================\r\n";
- fw = new FileWriter(s2,true);
- fr = new FileReader(s1);
-
- char [] buf = new char[1024];
- int len = 0;
- while ((len=fr.read(buf))!=-1){
- fw.write(buf,0,len);
- }
- fw.write(fuhao);
-
- }catch(IOException e ){
- throw new RuntimeException("读写失败!");
- }finally{
- if(fr!=null)
- try{
- //fr.flush();
- fr.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- if(fw!=null)
- try{
- fw.close();
- }catch(IOException e)
- {
- e.printStackTrace();
- }
-
- }
- }
- public static void listDe(String dirpath){
- File dir = new File(dirpath);
- String[] s = dir.list(new FilenameFilter(){
- public boolean accept(File dir ,String name){
- //文件名过滤;
- return name.endsWith(".java");
-
- }
- });
- for (String n:s){
- // System.out.println(dirpath+"\\"+n);
- Copy(dirpath+"\\"+n,s2);
- // System.out.println("=================================================================================");
- }
-
- }
- }
复制代码 /**把指定目录dirpath下的所有的java文件,(可以改其他文件)
续写到文件(绝对路径 s2)中。
需要的自己拿去用吧
|
|