|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
功能如题,具体信息请看代码中的注释,假如有不明白的地方可以到本站的Java版提问,我回尽快给出答复。 该软件主要实现了六个功能,拷贝、删除、根据指定的路径查找文件,根据指定的时间查找文件、根据参数建立目录和实现文件的移动。 package com.angel.filemanage; import java.io.*; import java.lang.Exception; import java.util.Vector; /********************************************** 功能说明: 实现文件的拷贝 ***********************************************/ public class copyFile { String sourcePath; String destinationPath; int buffer; long fileLength; FileInputStream sourceFile; FileOutputStream destinationFile; boolean makeDirs; boolean isMoving; boolean fieldSss; /*********************************************** 功能: 拷贝文件 使用说明: 参数含义: 无 返回值含义:-1:源文件不存在 -2:源文件是目录 -3:给定的目标路径不存在且不能创建路径 -4:拷贝时出错 1:拷贝成功 ************************************************/ protected int computeFunction() { File sourceFile = new File(sourcePath); File destFile = new File(destinationPath); File destinationFile=null; FileInputStream sourceFileInputStream = null; FileOutputStream destinationFileOutputStream = null; byte buf[] = new byte[buffer]; int counter = 0; if(!sourceFile.exists()) { return -1; } if(sourceFile.isDirectory()) //假如源文件是目录,抛出非常。 { return -2; } try { if(destFile.isDirectory()){ String srcName=sourceFile.getName(); if(destinationPath.endsWith("")==false) destinationPath+=""; destinationPath+=srcName; } destinationFile=new File(destinationPath); String parentDirectory = destinationFile.getParent(); File parentDirFile = new File(parentDirectory); if(!parentDirFile.exists()) { if(!makeDirs) //假如目标文件的目录不存在,又不能创建新目录,抛出非常。 { return -3; } if(makeDirs) //目标文件的目录不存在,则创建新目录。 parentDirFile.mkdirs(); //创建名为parentDirFile的目录。 } }catch(NullPointerException _ex) { } long oldFileLength = fileLength; fileLength = sourceFile.length(); try { sourceFileInputStream = new FileInputStream(sourceFile); destinationFileOutputStream = new FileOutputStream(destinationFile); while((counter = sourceFileInputStream.read(buf)) != -1) //从输入流中读buf长度的数据。 { destinationFileOutputStream.write(buf, 0, counter); //从buf中往输出流中写入counter长度的数据。 } } catch(IOException e) { System.out.println("拷贝文件时出错:"+e.toString()); return -4; } if(destinationFileOutputStream != null) try { destinationFileOutputStream.close(); //关闭输出流。 }catch(IOException e) { System.out.println("关闭目标文件时出错:"+e.toString()); return -4; } try { sourceFileInputStream.close(); //关闭输入流。 }catch(IOException e) { System.out.println("关闭源文件时出错:"+e.toString()); return -4; } return 1; } public int getBuffer() { return buffer; } public long getFileLength() { return fileLength; } /*********************************************** 功能: 得到目标路径 使用说明: 参数含义: 无 返回值含义:无 ************************************************/ public String getInputDestinationPath() { return destinationPath; } /*********************************************** 功能: 得到源路径 使用说明: 参数含义: 无 返回值含义:无 ************************************************/ public String getInputSourcePath() { return sourcePath; } public boolean getIsMoving() { return isMoving; } public boolean getMakeDirs() { return makeDirs; } public void setBuffer(int buffer) { this.buffer = buffer; } /*********************************************** 功能: 设置目标路径。 使用说明: 参数含义: destinationPath:目标路径 返回值含义:无 ************************************************/ public void setInputDestinationPath(String destinationPath) { this.destinationPath = destinationPath; } /*********************************************** 功能: 设置源路径。 使用说明: 参数含义: sourcePath:源路径 返回值含义:无 ************************************************/ public void setInputSourcePath(String sourcePath) { this.sourcePath = sourcePath; } public void setMakeDirs(boolean makeDirs) { boolean oldValue = this.makeDirs; this.makeDirs = makeDirs; } /*********************************************** 功能: 构造函数 使用说明: 参数含义: 无 返回值含义:无 ************************************************/ public copyFile() { sourcePath = ""; destinationPath = ""; buffer = 2048; fileLength = 0L; sourceFile = null; destinationFile = null; makeDirs = false; isMoving = false; fieldSss = false; } /*********************************************** 功能: 拷贝文件 使用说明: 参数含义: 无 返回值含义:-1:源文件不存在 -2:源文件是目录 -3:给定的目标路径不存在且不能创建路径 -4:拷贝时出错 1:拷贝成功 ************************************************/ public int copyFile(){ return computeFunction(); } } package com.angel.filemanage; import java.io.File; import java.io.PrintStream; import java.util.Vector; /** * 本文件归属man_angel * 本文件用于文件的删除 * 开发人员:angel * 开发日期:2002-03-16 */ /********************************************** 功能说明: ★ 删除文件 ***********************************************/ public class deleteFile { String pathToDelete; boolean recurse; File processFile; private boolean flag,flagdir; protected transient Vector aIOErrorEventListener; /*********************************************** 功能: 删除文件 使用说明: 参数含义: 无 返回值含义:-1:要删除的文件不存在 -2:不能删除该文件 -3:不能删除给定的目录 ************************************************/ protected int computeFunction() { if(!processFile.exists()){ System.out.println("删除文件时,要删除的文件不存在"); return -1; } if(processFile.isFile()) //是文件可以直接删除 { try { flag = processFile.delete(); //删除文件。 if(!flag) //假如不能删除文件,抛出非常。 return -2; }catch(SecurityException _ex) { System.out.println("删除文件时出错"); return -2; } } else{ if(processFile.isDirectory()) //假如要删除的文件是目录,调用deleteFolder()函数。 flag=deleteFolder(processFile); if(!flag) return -3; } return 1; } /*********************************************** 功能: 删除目录 使用说明: 参数含义: processFolder:要删除的目录 返回值含义:无 ************************************************/ private boolean deleteFolder(File processFolder) { String fileList[] = processFolder.list(); //返回processFolder指定的目录中的文件。 if(fileList.length == 0) //是空目录,可以直接删除 processFolder.delete(); else { for(int i = 0; i < fileList.length; i++){ File fileDel = new File(processFolder.getPath() + System.getProperty("file.separator") + fileList[i]); if(fileDel.isFile()){ try { flag = fileDel.delete(); if(!flag) return false; }catch(SecurityException _ex){ System.out.println("删除文件时出错"); return false; } } else if(fileDel.isDirectory() && recurse) flagdir=deleteFolder(fileDel); if(!flagdir) return false; } flag=processFolder.delete(); if(!flag) return false; } return true; } public String getInputPathToDelete() { if(pathToDelete == null) pathToDelete = new String(); return pathToDelete; } public boolean getRecurse() { return recurse; } public void setInputPathToDelete(String pathToDelete) { this.pathToDelete = pathToDelete; try { processFile = new File(pathToDelete); }catch(NullPointerException _ex) { System.out.println("指定的路径不存在!"); } } public void setRecurse(boolean recurse) { boolean _tmp = this.recurse; this.recurse = recurse; } /*********************************************** 功能: 构造函数 使用说明: 参数含义: 无 返回值含义:无 ************************************************/ public deleteFile() { pathToDelete = ""; recurse = false; processFile = null; flag = false; flagdir=false; } /*********************************************** 功能: 删除文件,提供应外界的接口 使用说明: 参数含义: 无 返回值含义:-1:要删除的文件不存在 -2:不能删除该文件 -3:不能删除给定的目录 ************************************************/ public int removeFile(){ return computeFunction(); } } package com.angel.filemanage; import java.io.*; import java.util.Vector; import java.awt.List; /** * 本文件归属man_angel * 本文件用于查找文件 * 开发人员:angel * 开发日期:2002-03-11 */ /********************************************** 功能说明: ★ 根据指定的路径,查找文件 ***********************************************/ public class findFile { private Vector oldSearchResult; private Vector searchResult; private Vector operations; private Vector strings; private String arbString; private String arbChar; String emptyString; String fieldIllegalSimbols; String fieldMultipleCharacterPattern; String fieldSingleCharacterPattern; boolean fieldRecurse; String fieldResultLastFoundFilename; String fieldInputPath; String fieldInputPattern; String fieldResultListOfFilenames[]; boolean patExtEmpty; boolean tagExtEmpty; int fieldResultCountOfFoundFiles; private boolean abort; protected String[] result; public void abort() { abort = true; } /*********************************************** 功能: 查找文件 使用说明: 参数含义: 无 返回值含义:-1:搜索路径不存在或不是个目录 -2:搜索失败 1:搜索成功 ************************************************/ protected int computeFunction() { oldSearchResult = (Vector)searchResult.clone(); searchResult.removeAllElements(); if(fieldMultipleCharacterPattern.equals("")) return -2; if(fieldSingleCharacterPattern.equals("")) return -2; for(int i = 0; i < fieldIllegalSimbols.length(); i++) if(fieldInputPath.indexOf(fieldIllegalSimbols.charAt(i)) != -1) return -2; File path = new File(fieldInputPath); if(!path.exists() || !path.isDirectory()) //假如搜索路径不存在或不是个目录 return -1; searchFolder(fieldInputPath); if(!searchResult.isEmpty()) { String arr[] = new String[searchResult.size()]; searchResult.copyInto(arr); fieldResultListOfFilenames = new String[arr.length]; System.arraycopy(arr, 0, fieldResultListOfFilenames, 0, arr.length); fieldResultCountOfFoundFiles = searchResult.size(); } return 1; } public String getIllegalSymbols() { if(fieldIllegalSimbols == null) try { fieldIllegalSimbols = new String(); } catch(Throwable _ex) { System.err.println("Exception creating illegalSimbolsproperty."); } return fieldIllegalSimbols; } public String getInputPath() { if(fieldInputPath == null) try { fieldInputPath = new String(); } catch(Throwable _ex) { System.err.println("Exception creating inputPathproperty."); } return fieldInputPath; } public String getInputPattern() { if(fieldInputPattern == null) try { fieldInputPattern = new String(); } catch(Throwable _ex) { System.err.println("Exception creating inputPatternproperty."); } return fieldInputPattern; } public String getMultipleCharacterPattern() { if(fieldMultipleCharacterPattern == null) try { fieldMultipleCharacterPattern = new String(); } catch(Throwable _ex) { System.err.println("Exception creating multipleCharacterPatternproperty."); } return fieldMultipleCharacterPattern; } public boolean getRecurse() { return fieldRecurse; } public int getResultCountOfFoundFiles() { fieldResultCountOfFoundFiles = searchResult.size(); return fieldResultCountOfFoundFiles; } public String getResultLastFoundFilename() { if(fieldResultLastFoundFilename == null) try { fieldResultLastFoundFilename = new String(); } catch(Throwable _ex) { System.err.println("Exception creating resultLastFoundFilenameproperty."); } return fieldResultLastFoundFilename; } public String[] getResultListOfFilenames() { return fieldResultListOfFilenames; } public String getSingleCharacterPattern() { if(fieldSingleCharacterPattern == null) try { fieldSingleCharacterPattern = new String(); } catch(Throwable _ex) { System.err.println("Exception creating singleCharacterPatternproperty."); } return fieldSingleCharacterPattern; } /*********************************************** 功能: 匹配函数 使用说明: 参数含义: aTarget:要匹配的字符串 aPattern:匹配模式 返回值含义:假如匹配成功,返回true;反之,返回false ************************************************/ private boolean match(String aTarget, String aPattern) { String target = aTarget; String pattern = aPattern; if(aTarget == null) throw new IllegalArgumentException("aTarget is null"); if(aPattern == null) throw new IllegalArgumentException("aPattern is null"); if(pattern.equals("*.*")) pattern = "*"; if(pattern.equals(arbString)) return true; if(target.equals(pattern)) return true; if(patExtEmpty && target.length() == 1 && tagExtEmpty && pattern.equals(".")) { patExtEmpty = false; tagExtEmpty = false; return true; } if(target.equals(emptyString)) //要匹配的串为空 return false; if(pattern.equals(emptyString)) //匹配模式为空 return false; //arbString 初始值为"*" arbChar初始值为"?" if(pattern.substring(0, 1).equals(arbString) && pattern.substring(1, 2).equals(arbString)) pattern = pattern.substring(1); else if(pattern.substring(0, 1).equals(arbString) && pattern.substring(1, 2).equals(arbChar)) { pattern = arbChar + arbString + pattern.substring(2); } else { if(pattern.substring(0, 1).equals(arbChar)) //假如匹配模式的第一个字符为´?´,则从第二个字符开始比较 return match(target.substring(1), pattern.substring(1)); if(pattern.substring(0, 1).equals(arbString)) { for(int i = 0; i < target.length(); i++) if(match(target.substring(i), pattern.substring(1))) return true; return false; } if(pattern.substring(0, 1).equals(target.substring(0, 1))) return match(target.substring(1), pattern.substring(1)); else return false; } return match(target, pattern); } /*********************************************** 功能: 搜索指定路径的文件。 使用说明: 参数含义: path:指定要搜索的路径 返回值含义:无 ************************************************/ private void searchFolder(String path) { File f = new File(path); String list[] = f.list(); //返回由path指定路径里的所有文件名和目录名 if(list != null){ for(int i = 0; i < list.length; i++) { File file = new File(path + list[i]); if(file.isFile()) { if(list[i].indexOf(".") == -1) //文件没有后缀 tagExtEmpty = true; else tagExtEmpty = false; if(fieldInputPattern.charAt(fieldInputPattern.length() - 1) == ´.´) patExtEmpty = true; if(match(list[i], fieldInputPattern)) { String resultPath = file.getPath(); int _tmp = fieldResultCountOfFoundFiles; searchResult.addElement(resultPath); String oldResultLastFoundFilename = fieldResultLastFoundFilename; fieldResultLastFoundFilename = resultPath; fieldResultCountOfFoundFiles = searchResult.size(); } } else if(fieldRecurse) { searchFolder(path + list[i] + System.getProperty("file.separator")); } } } } public void setIllegalSymbols(String illegalSymbols) { String oldValue = fieldIllegalSimbols; fieldIllegalSimbols = illegalSymbols; } public void setInputPath(String inputPath) { if(inputPath == null) throw new IllegalArgumentException("Can not set inputPath to null!"); if(!inputPath.endsWith(System.getProperty("file.separator")) && !inputPath.endsWith("/") && inputPath.length() > 0) inputPath = inputPath + System.getProperty("file.separator"); fieldInputPath = inputPath; } /*********************************************** 功能: 设置匹配模式 使用说明: 参数含义: inputPattern:匹配模式 返回值含义:无 ************************************************/ public void setInputPattern(String inputPattern) { if(inputPattern == null || inputPattern.length() == 0) return; if(inputPattern == ".") return; fieldInputPattern = inputPattern; } public void setMultipleCharacterPattern(String multipleCharacterPattern) { String _tmp = fieldMultipleCharacterPattern; fieldMultipleCharacterPattern = multipleCharacterPattern; if(multipleCharacterPattern == null || multipleCharacterPattern.length() == 0) throw new IllegalArgumentException("Can not set multipleCharacterPattern to null!"); fieldMultipleCharacterPattern = multipleCharacterPattern; if(multipleCharacterPattern.length() == 1) { arbString = multipleCharacterPattern; } else { arbString = multipleCharacterPattern.substring(0, 1); fieldMultipleCharacterPattern = arbString; } } /*********************************************** 功能: 是否搜索子文件夹 使用说明: 参数含义: recurse:true ,搜索 false,不搜索 返回值含义:无 ************************************************/ public void setRecurse(boolean recurse) { boolean oldValue = fieldRecurse; fieldRecurse = recurse; } public void setSingleCharacterPattern(String singleCharacterPattern) { String _tmp = fieldSingleCharacterPattern; if(singleCharacterPattern == null || singleCharacterPattern.length() == 0) throw new IllegalArgumentException("Can not set singleCharacterPattern to null!"); if(singleCharacterPattern.length() == 1) fieldSingleCharacterPattern = singleCharacterPattern; else fieldSingleCharacterPattern = singleCharacterPattern.substring(0, 1); arbChar = fieldSingleCharacterPattern; } /*********************************************** 功能: 搜索文件 使用说明: 参数含义: 无 返回值含义:结果集 -1:搜索路径不存在或不是个目录 -2:搜索失败 1:搜索成功 ************************************************/ public Vector findFile(){ if(computeFunction()==1) return searchResult; else{ searchResult.clear(); if(computeFunction()==-1){ searchResult.addElement("-1"); }else{ searchResult.addElement("-2"); } } return searchResult; } public findFile() { oldSearchResult = new Vector(); searchResult = new Vector(10); operations = new Vector(); strings = new Vector(); arbString = "*"; arbChar = "?"; emptyString = ""; fieldIllegalSimbols = "<>|"; fieldMultipleCharacterPattern = "*"; fieldSingleCharacterPattern = "?"; fieldRecurse = false; fieldResultLastFoundFilename = ""; fieldInputPath = ""; fieldInputPattern = ""; fieldResultListOfFilenames = new String[0]; patExtEmpty = false; tagExtEmpty = false; fieldResultCountOfFoundFiles = 0; abort = false; } } package com.angel.filemanage; import java.util.Vector; import java.io.File; import java.util.Date; /** * 本文件归属man_angel * 本文件用于查找文件 * 开发人员:angel * 开发日期:2002-03-11 */ /********************************************** 功能说明: ★ 根据指定的时间,查找文件 ***********************************************/ public class findFileByTime { private Vector searchResult; //保存搜索结果 boolean fieldRecurse; //是否搜索子文件夹 long startTime; long endTime; public Vector searchFile(String path){ //searchResult.removeAllElements(); File file1 = new File(path); if(file1.isDirectory()==true) searchFolder(path); return searchResult; } /*********************************************** 功能: 是否搜索子文件夹 使用说明: 参数含义: recurse:true ,搜索 false,不搜索 返回值含义:无 ************************************************/ public void setRecurse(boolean recurse) { boolean oldValue = fieldRecurse; fieldRecurse = recurse; } /*********************************************** 功能: 搜索指定路径的文件。 使用说明: 参数含义: path:指定要搜索的路径 返回值含义:无 ************************************************/ private void searchFolder(String path) { if(path.endsWith("")==false) path=path+""; File f = new File(path); String list[] = f.list(); //返回由path指定路径里的所有文件名和目录名 if(list != null){ for(int i = 0; i < list.length; i++) { File file = new File(path +list[i]); if(file.isFile()){ if(match(file)){ String resultPath = file.getPath(); searchResult.addElement(resultPath); } }else if(fieldRecurse) { searchFolder(path + list[i] + System.getProperty("file.separator")); } } } } public void setStartTime(long t){ startTime=t; } public void setEndTime(long t){ endTime=t; } private boolean match(File file){ long time=file.lastModified(); Date date=new Date(time); if(startTime==0){ //搜索endTime时间之前的所有文件 if(time<=endTime) return true; } else if(endTime==0){ //搜索startTime时间之后的所有文件 if(time>=startTime) return true; } else if(time>=startTime&&time<=endTime) return true; return false; } public findFileByTime(){ startTime=0; endTime=0; fieldRecurse=false; searchResult = new Vector(); } } package com.angel.filemanage; import java.io.*; /** * 本文件归属man_angel * 本文件用于建立目录 * 开发人员:angel * 开发日期:2002-03-11 */ /********************************************** 功能说明: ★ 根据参数建立目录。 ***********************************************/ public class mkDir{ /*********************************************** 功能: 构造函数 使用说明: 参数含义: 无 返回值含义:无 ************************************************/ public mkDir(){ } /*********************************************** 功能: 建立目录 使用说明: 参数含义: pathName:指定要建立的目录 返回值含义:是否成功创建的标志 ************************************************/ public boolean createDir(String pathName){ File parentDirFile = new File(pathName); if(!parentDirFile.exists()){ try{ parentDirFile.mkdirs(); //创建名为parentDirFile的目录。 }catch(NullPointerException e) { System.out.println("创建目录时失败:"+e.toString()); return false; } } else{ System.out.println("指定的目录已存在!"); return false; } return true; } } package com.angel.filemanage; import java.io.*; import java.util.Vector; /** * 本文件归属man_angel * 本文件用于移动文件 * 开发人员:angel * 开发日期:2002-03-11 */ /********************************************** 功能说明: ★ 实现文件的移动 ***********************************************/ public class moveFile { String sourcePath; String destinationPath; int buffer; long fileLength; FileInputStream sourceFile; FileOutputStream destinationFile; boolean makeDirs; /*********************************************** 功能: 移动文件 使用说明: 参数含义: 无 返回值含义:-1:源文件不存在 -2:源文件是目录 -3:目标路径为空 -4:目标文件已存在 -5:目标目录不存在,且不能创建新目录 -6:移动文件出错 1:移动文件成功 ************************************************/ protected int computeFunction() { File sourceFile = new File(sourcePath); File destFile = new File(destinationPath); File destinationFile=null; FileInputStream sourceFileInputStream = null; FileOutputStream destinationFileOutputStream = null; byte buf[] = new byte[buffer]; int counter = 0; if(sourcePath.length() == 0 || !sourceFile.exists()) //假如源文件不存在,抛出非常。 { return -1; } if(sourceFile.isDirectory()) //假如源文件是目录,抛出非常。 { return -2; } if(destinationPath.length() == 0 ) { return -3; } if(destFile.isFile()&&destFile.exists()){ return -4; } try { if(destFile.isDirectory()==true){ String srcName=sourceFile.getName(); if(destinationPath.endsWith("")==false) destinationPath+=""; destinationPath+=srcName; } destinationFile=new File(destinationPath); String parentDirectory = destinationFile.getParent(); File parentDirFile = new File(parentDirectory); if(!parentDirFile.exists()) { if(!makeDirs) { return -5; } if(makeDirs) parentDirFile.mkdirs(); } }catch(NullPointerException _ex) { } long oldFileLength = fileLength; fileLength = sourceFile.length(); try{ sourceFileInputStream = new FileInputStream(sourceFile); destinationFileOutputStream = new FileOutputStream(destinationFile); while((counter = sourceFileInputStream.read(buf)) != -1) { destinationFileOutputStream.write(buf, 0, counter); } }catch(IOException e) { System.out.println("移动文件时出错:"+e.toString()); return -6; } if(destinationFileOutputStream != null) try { destinationFileOutputStream.close(); }catch(IOException e) { System.out.println("移动文件:关闭目标文件时出错:"+e.toString()); return -6; } try{ sourceFileInputStream.close(); }catch(IOException e) { System.out.println("移动文件:关闭源文件时出错:"+e.toString()); return -6; } if(!sourceFile.delete()){ System.out.println("移动文件:不能移动源文件"); return -6; } return 1; } public int getBuffer() { return buffer; } public long getFileLength() { return fileLength; } public String getInputDestinationPath() { return destinationPath; } public String getInputSourcePath() { return sourcePath; } public boolean getMakeDirs() { return makeDirs; } public void setBuffer(int buffer) { this.buffer = buffer; } public void setInputDestinationPath(String destinationPath) { this.destinationPath = destinationPath; } public void setInputSourcePath(String sourcePath) { this.sourcePath = sourcePath; } public void setMakeDirs(boolean makeDirs) { boolean oldValue = this.makeDirs; this.makeDirs = makeDirs; } public moveFile() { sourcePath = ""; destinationPath = ""; buffer = 2048; fileLength = 0L; sourceFile = null; destinationFile = null; makeDirs = false; } public int moveFile(){ return computeFunction(); } } 返回类别: 教程 上一教程: 一个用JSP做的日历 下一教程: 用JAVA开发EMAIL工具之发送邮件 您可以阅读与"一个用JAVA实现了文件基本治理的软件"相关的教程: · 用java实现外部调用exe文件 · 用Java实现HTTP文件队列下载 · JavaBean实现多文件上传的两种方式 · JAVABEAN实现多文件上传的两种方式 · 用JAVA实现一个分页类 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |