java文件操作

来源:互联网 发布:淘宝联盟 提现 手续费 编辑:程序博客网 时间:2024/05/27 09:45

背景:前些日子要用jmf插件做个“视频监控系统”,遇到了一些问题,这问题和http://topic.csdn.net/t/20060507/23/4734935.html的帖子相关,为了验证此帖子的#18楼所说的:

jmf安装文件后,在windows/system32下面,生成一共有21个 的dll,如下:
jmacm.dll
jmam.dll
jmcvid.dll
jmdaud.dll
jmdaudc.dll
jmddraw.dll
jmfjawt.dll
jmg723.dll
jmgdi.dll
jmgsm.dll
jmh261.dll
jmh263enc.dll
jmjpeg.dll
jmmci.dll
jmmpa.dll
jmmpegv.dll
jmutil.dll
jmvcm.dll
jmvfw.dll
jmvh263.dll
jsound.dll
所以我写了以下代码文件,主要是为了对java文件操作的熟悉。

GetFile.java

Code:
  1. /*  
  2.  
  3. 功能: A.列出文件 B.对比文件  C.复制  
  4.  
  5. */  
  6.   
  7. import java.io.*;   
  8. import java.util.Scanner;   
  9. import java.util.ArrayList;   
  10. public class GetFile{   
  11.  static ArrayList<String> fileNames = new ArrayList<String>();   
  12.  public void getAllFile(String dirName){   
  13.   File file = new File(dirName);   
  14.   if(file.exists()){   
  15.    //判断是否为文件   
  16.    if(file.isFile()){   
  17.     //输出文件名   
  18.     //System.out.println("-" + dirName);   
  19.     String fileName = file.getName();   
  20.     System.out.println("-" + fileName);   
  21.     fileNames.add(fileName);   
  22.     return;   
  23.    }   
  24.    if(file.isDirectory()){   
  25.     //输出目录名   
  26.     System.out.println("+" + dirName);   
  27.     //列出目录所有文件   
  28.     String[] allContent = file.list();   
  29.     if(allContent==null){   
  30.      //文件受保护   
  31.      return;   
  32.     }   
  33.     for(String i : allContent){   
  34.      //递归   
  35.      getAllFile(dirName + "/" + i);   
  36.     }   
  37.    }   
  38.   }   
  39.  }   
  40.     
  41.  public void write(String s){   
  42.   try{   
  43.    PrintWriter pw = new PrintWriter(new FileOutputStream("info.txt"),true);   
  44.    pw.println(s);   
  45.    pw.close();   
  46.   }   
  47.   catch(FileNotFoundException fe){   
  48.    fe.printStackTrace();   
  49.   }   
  50.  }   
  51.     
  52.  public void pK(ArrayList<String> fileNameList){   
  53.   File file = new File("info.txt");   
  54.   if(file.exists()&&file.isFile()){   
  55.    try{   
  56.     //把info.txt读入数组   
  57.     ArrayList<String> txtlines = new ArrayList<String>();   
  58.     BufferedReader reader = new BufferedReader(new FileReader(file));   
  59.     String line = reader.readLine();   
  60.     while(line != null){   
  61.      txtlines.add(line);   
  62.      line = reader.readLine();   
  63.     }   
  64.     reader.close();   
  65.     //对比,得出不同的文件   
  66.     PrintWriter pw = new PrintWriter(new FileOutputStream("pkinfo.txt"),true);   
  67.     for(int i=0;i<fileNameList.size();i++){   
  68.      String fileName = fileNameList.get(i);   
  69.      if(txtlines.indexOf(fileName)==-1){   
  70.       System.out.println("新文件:/n" + i + "=" + fileName + "/n");   
  71.       pw.println(fileName + "/n");   
  72.      }   
  73.     }   
  74.     pw.close();   
  75.    }   
  76.    catch(IOException ie){   
  77.     ie.printStackTrace();   
  78.    }   
  79.   }   
  80.   else{   
  81.    System.out.println("info.txt 不存在!");   
  82.   }   
  83.  }   
  84.     
  85.  public void copyAll(String dir){   
  86.   File file = new File("pkinfo.txt");   
  87.   if(file.exists()&&file.isDirectory()){   
  88.    try{   
  89.     BufferedReader reader = new BufferedReader(new FileReader(file));   
  90.     String line = reader.readLine();   
  91.     while(line != null){   
  92.      //复制文件   
  93.      copy(dir + "/" +  line,line,true);   
  94.      line = reader.readLine();   
  95.     }   
  96.     reader.close();   
  97.        
  98.    }   
  99.    catch(IOException ie){   
  100.     ie.printStackTrace();   
  101.    }   
  102.   }   
  103.  }   
  104.     
  105.  //复制文件     源文件路径 目标路径  当已存在时,是否强行覆盖。   
  106.  public static boolean copy(String srcPath,String otherPath,boolean over){   
  107.   try{   
  108.    File srcFile = new File(srcPath);   
  109.    if((!srcFile.exists())||(!srcFile.isFile())){   
  110.     return false;   
  111.    }   
  112.    File otherFile = new File(otherPath);   
  113.    if(otherFile.exists()){   
  114.     if(over==true){   
  115.      otherFile.delete();   
  116.     }   
  117.     else{   
  118.      return false;   
  119.     }   
  120.    }   
  121.    FileOutputStream fos = new FileOutputStream(otherFile);   
  122.    FileInputStream fis = new FileInputStream(srcFile);     
  123.          byte[] buf = new byte[1024];       
  124.          int num = -1;      
  125.          while((num = fis.read(buf,0,buf.length))!=(-1)) {        
  126.           fos.write(buf,0,num);       
  127.           //fos.flush();    
  128.          }   
  129.          fos.flush();   
  130.          fos.close();   
  131.          fis.close();   
  132.          return true;   
  133.   }   
  134.   catch(Exception e){   
  135.    return false;   
  136.   }   
  137.  }   
  138.     
  139.  public static void main(String[] args){   
  140.   System.out.println("请选择操作:/nA.列出文件/nB.对比文件/nC.复制");   
  141.   /*try{  
  142.    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));  
  143.    String dirName = reader.readLine();  
  144.    reader.close();  
  145.   }  
  146.   catch(IOException ie){  
  147.    ie.printStackTrace();  
  148.   }*/  
  149.   Scanner sc = new Scanner(System.in);   
  150.   String choose = sc.nextLine();   
  151.   System.out.println("请输入目录名:[如C:/windows/system32]");   
  152.   String dirName = sc.nextLine();   
  153.   sc.close();   
  154.   GetFile getFile = new GetFile();   
  155.   getFile.getAllFile(dirName);   
  156.   if(choose.equals("A")){   
  157.    String str = "";   
  158.    for(int i=0;i<fileNames.size();i++){   
  159.     str = str + fileNames.get(i) + "/n";   
  160.        
  161.    }   
  162.    getFile.write(str);   
  163.   }   
  164.   else if(choose.equals("B")){   
  165.    getFile.pK(fileNames);   
  166.   }   
  167.   else{   
  168.    getFile.copyAll(dirName);   
  169.   }   
  170.  }   
  171. }   
  172.   

本文是个人原创,如文中有错或你有建议,请留言指出,如要交流请加QQ519870018,如要转载本文,请标明本文出处。

这本是我的csdn的博客,转到此学生大本营和大家分享一下,想大家留言讨论一下,支持一下。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CpuSheep/archive/2010/08/30/5850620.aspx