- /**************************************
- * Title: 查找指定目录文件
- * Description: FileFilter类
- * date : 2006-3-25
- * author : zhanglei
- ***************************************/
- package server;
- import java.io.*;
- public class findfile {
- private String pathname;
- private DomXML DomXML = MainServer.DomXML;
- public findfile(String pathname) {
- this.pathname = pathname;
- }
- private static FileFilter fileFilter = new FileFilter() {
- public boolean accept(File pathname) {
- String tmp = pathname.getName().toLowerCase();
- //if(tmp.endsWith(".xml")){
- if (tmp.indexOf(".") != -1) {
- //System.out.println(tmp);
- return true;
- }
- //}
- return false;
- }
- };
- //获得文件列表
- public File[] GetFile() {
- try {
- File root = new File(pathname);
- File[] files = root.listFiles(fileFilter);
- return files;
- }
- catch (Exception e) {
- System.out.println("GetFile" + pathname + "文件夹不存在!" + e);
- return null;
- }
- /* for(int i=0; i<files.length; i++)
- {
- System.out.println(i + "name " + files[i]);
- }*/
- }
- /**
- *BackFile 备份XML文件
- *@param1 doFile 被移动文件
- *@param2 return 备份后文件
- */
- public File BackFile(File yFile) {
- File bakFile = null; //备份文件
- File bakDir = null; //备份文件路径
- File doDir = null; //备份文件路径
- File doFile = null; //移动后的处理文件
- String bakPath = "";
- String recvPath = "";
- try {
- //创建备份路径
- DomXML.GetBeginItem("PathConfig");
- if (DomXML.GetItemAttribute("bak_dir", "run").toLowerCase().trim().equals("y")) {
- bakPath = DomXML.GetItemAttribute("bak_dir", "path");
- bakDir = new File(bakPath + "/" + MyFunction.DataTimeStr("yyyyMMdd"));
- if (!bakDir.exists()) {
- bakDir.mkdirs();
- }
- //备份XML文件
- bakFile = new File(bakDir.getPath() + "/" + yFile.getName());
- if (bakFile.exists()) {
- bakFile.delete();
- }
- MyFunction.CopyFile(yFile.toString(), bakFile.toString());
- }
- //移动目标文件
- recvPath = DomXML.GetItemAttribute("recv_up", "path").trim();
- doDir = new File(recvPath);
- if (!doDir.exists()) {
- doDir.mkdirs();
- }
- doFile = new File(recvPath + "/" + yFile.getName());
- if (doFile.exists()) {
- doFile.delete();
- }
- yFile.renameTo(doFile);
- return doFile;
- }
- catch (Exception e) {
- System.out.println(e + "文件备份失败!");
- return null;
- }
- }
- /**
- *MoveErrorFile 移动异常XML文件
- *@param1 doFile 被移动文件
- *@param2 return 0正确,-1错误
- */
- public int MoveErrorFile(File doFile) {
- File errorFile = null;
- File errorDir = null; //异常文件路径
- try {
- DomXML.GetBeginItem("PathConfig");
- if (DomXML.GetItemAttribute("error_dir", "run").trim().toUpperCase().equals("Y")) {
- errorDir = new File(DomXML.GetItemAttribute("error_dir", "path").trim() + "/" + MyFunction.DataTimeStr("yyyyMMdd"));
- if (!errorDir.exists()) {
- errorDir.mkdirs();
- }
- errorFile = new File(errorDir.getPath() + "/" + doFile.getName());
- if (errorFile.exists()) {
- errorFile.delete();
- }
- doFile.renameTo(errorFile);
- }
- else {
- doFile.delete();
- }
- return 0;
- }
- catch (Exception e) {
- System.out.println(e + "移动异常文件失败!");
- return -1;
- }
- }
- public static void main(String[] args) {
- File bakFile = new File("D:/JB_Work/XmlServer/ErrorXmlFile/test36.xml");
- findfile FindFile = new findfile("D:/JB_Work/XmlServer/recv_dir");
- FindFile.MoveErrorFile(bakFile);
- }
- }
findfile.java
最新推荐文章于 2022-12-14 21:04:05 发布