背景:前些日子要用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
- /*
- 功能: A.列出文件 B.对比文件 C.复制
- */
- import java.io.*;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class GetFile{
- static ArrayList<String> fileNames = new ArrayList<String>();
- public void getAllFile(String dirName){
- File file = new File(dirName);
- if(file.exists()){
- //判断是否为文件
- if(file.isFile()){
- //输出文件名
- //System.out.println("-" + dirName);
- String fileName = file.getName();
- System.out.println("-" + fileName);
- fileNames.add(fileName);
- return;
- }
- if(file.isDirectory()){
- //输出目录名
- System.out.println("+" + dirName);
- //列出目录所有文件
- String[] allContent = file.list();
- if(allContent==null){
- //文件受保护
- return;
- }
- for(String i : allContent){
- //递归
- getAllFile(dirName + "/" + i);
- }
- }
- }
- }
- public void write(String s){
- try{
- PrintWriter pw = new PrintWriter(new FileOutputStream("info.txt"),true);
- pw.println(s);
- pw.close();
- }
- catch(FileNotFoundException fe){
- fe.printStackTrace();
- }
- }
- public void pK(ArrayList<String> fileNameList){
- File file = new File("info.txt");
- if(file.exists()&&file.isFile()){
- try{
- //把info.txt读入数组
- ArrayList<String> txtlines = new ArrayList<String>();
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = reader.readLine();
- while(line != null){
- txtlines.add(line);
- line = reader.readLine();
- }
- reader.close();
- //对比,得出不同的文件
- PrintWriter pw = new PrintWriter(new FileOutputStream("pkinfo.txt"),true);
- for(int i=0;i<fileNameList.size();i++){
- String fileName = fileNameList.get(i);
- if(txtlines.indexOf(fileName)==-1){
- System.out.println("新文件:/n" + i + "=" + fileName + "/n");
- pw.println(fileName + "/n");
- }
- }
- pw.close();
- }
- catch(IOException ie){
- ie.printStackTrace();
- }
- }
- else{
- System.out.println("info.txt 不存在!");
- }
- }
- public void copyAll(String dir){
- File file = new File("pkinfo.txt");
- if(file.exists()&&file.isDirectory()){
- try{
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = reader.readLine();
- while(line != null){
- //复制文件
- copy(dir + "/" + line,line,true);
- line = reader.readLine();
- }
- reader.close();
- }
- catch(IOException ie){
- ie.printStackTrace();
- }
- }
- }
- //复制文件 源文件路径 目标路径 当已存在时,是否强行覆盖。
- public static boolean copy(String srcPath,String otherPath,boolean over){
- try{
- File srcFile = new File(srcPath);
- if((!srcFile.exists())||(!srcFile.isFile())){
- return false;
- }
- File otherFile = new File(otherPath);
- if(otherFile.exists()){
- if(over==true){
- otherFile.delete();
- }
- else{
- return false;
- }
- }
- FileOutputStream fos = new FileOutputStream(otherFile);
- FileInputStream fis = new FileInputStream(srcFile);
- byte[] buf = new byte[1024];
- int num = -1;
- while((num = fis.read(buf,0,buf.length))!=(-1)) {
- fos.write(buf,0,num);
- //fos.flush();
- }
- fos.flush();
- fos.close();
- fis.close();
- return true;
- }
- catch(Exception e){
- return false;
- }
- }
- public static void main(String[] args){
- System.out.println("请选择操作:/nA.列出文件/nB.对比文件/nC.复制");
- /*try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String dirName = reader.readLine();
- reader.close();
- }
- catch(IOException ie){
- ie.printStackTrace();
- }*/
- Scanner sc = new Scanner(System.in);
- String choose = sc.nextLine();
- System.out.println("请输入目录名:[如C:/windows/system32]");
- String dirName = sc.nextLine();
- sc.close();
- GetFile getFile = new GetFile();
- getFile.getAllFile(dirName);
- if(choose.equals("A")){
- String str = "";
- for(int i=0;i<fileNames.size();i++){
- str = str + fileNames.get(i) + "/n";
- }
- getFile.write(str);
- }
- else if(choose.equals("B")){
- getFile.pK(fileNames);
- }
- else{
- getFile.copyAll(dirName);
- }
- }
- }
本文是个人原创,如文中有错或你有建议,请留言指出,如要交流请加QQ519870018,如要转载本文,请标明本文出处。
这本是我的csdn的博客,转到此学生大本营和大家分享一下,想大家留言讨论一下,支持一下。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CpuSheep/archive/2010/08/30/5850620.aspx