Java单线程,频繁操作文件问题。
//线程调用的方法public static boolean checkLogFileInfos(String path, String fileName) { boolean bool = false; // 判断fileName在Path中是否存在 isExistTxtFile(path, fileName); File file = new File(path + "\\" + fileName); // 如果打印信息的日志文件的大小大于1024KB if ((file.length() / 1024) >=12)) { // 首先建立临时文件夹 System.out.println("创建临时文件夹" + path + "\\" + "TEMPFILES"); File tempFile = new File(path + "\\" + "TEMPFILES"); tempFile.mkdir(); // 将文件转移到临时文件夹中 System.out.println("将文件" + file.getName() + "转移到" + tempFile.getName() + "中"); copyFiletoPath(path, path + "\\" + properties.getProperty("LOG_TEMP_FILE_NAME"), fileName, ""); // 将临时文件夹中的文件进行重命名,将临时文件夹中的文件转移到上一级目录中。 File tempFileTxt = new File(path + "\\" + properties.getProperty("LOG_TEMP_FILE_NAME") + "\\" + fileName); String newNameStr = newName(fileName); File newTempFileTxt = new File(path + "\\" + newNameStr); System.out.println("开始重命名" + tempFileTxt.getPath()); bool = tempFileTxt.renameTo(newTempFileTxt); if (bool) { System.out.println(tempFileTxt.getName() + "重命名成功" + bool); } else { System.out.println(tempFileTxt.getName() + "重命名失败" + bool); } // 建立新的文件 boolean bool1 = file.delete(); System.out.println("删除" + file.getPath() + "的结果是" + bool1); System.out.println("开始创建新的文件" + file.getPath()); try { file.createNewFile(); } catch (IOException e) { System.out.println("创建" + file.getName() + "失败" + bool); e.printStackTrace(); } // 删除临时文件夹 System.out.println("删除" + tempFile.getPath()); boolean bool2 = tempFile.delete(); System.out.println("删除" + tempFile.getPath() + "结果是" + bool2); boolean bool3 = tempFileTxt.delete(); System.out.println("删除" + tempFileTxt.getPath() + "结果是" + bool3); } return bool; }//相关的方法: private static String newName(String fileName) { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss"); String dateStr = format.format(date); return dateStr + "" + fileName; } private static void isExistTxtFile(String path, String fileName) { boolean bool = false; File file = new File(path); File[] files = file.listFiles(); if (files.length > 0) { for (int i = 0; i < files.length; i++) { if (files[i].getName().equals(fileName)) { bool = true; } } if (!bool) { File newFile = new File(path + "\\" + fileName); try { boolean isOkNewFile = newFile.createNewFile(); if (isOkNewFile) { System.out.println("文件创建成功"); } else { System.out.println("文件创建失败"); } } catch (Exception e) { e.printStackTrace(); } } } else { bool = false; File newFile = new File(path + "\\" + fileName); try { boolean isOkNewFile = newFile.createNewFile(); if (isOkNewFile) { System.out.println("文件创建成功"); } else { System.out.println("文件创建失败"); } } catch (Exception e) { e.printStackTrace(); } } } public static String returnStardardTime() { String stardardTime = ""; Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(properties .getProperty("TIME_TYPE")); stardardTime = simpleDateFormat.format(date); return stardardTime; } public static void copyFiletoPath(String fromPath, String toPath, String fileName, String newFileName) { int bytesum = 0; int byteread = 0; if (fileName.equals("") || fileName != null) { File oldFile = new File(fromPath + "/" + fileName); File newFile = null; if (newFileName.equals("") || newFileName == null) { newFile = new File(toPath + "/" + fileName); } else { newFile = new File(toPath + "/" + newFileName); } try { if (oldFile.exists()) { FileInputStream fileInputStream = new FileInputStream( oldFile); FileOutputStream fileOutputStream = new FileOutputStream( newFile); byte[] buffer = new byte[1024]; while ((byteread = fileInputStream.read(buffer)) != -1) { bytesum += byteread; fileOutputStream.write(buffer, 0, byteread); } fileInputStream.close(); fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } }private static void isExistTxtFile(String path, String fileName) { boolean bool = false; File file = new File(path); File[] files = file.listFiles(); if (files.length > 0) { for (int i = 0; i < files.length; i++) { if (files[i].getName().equals(fileName)) { bool = true; } } if (!bool) { File newFile = new File(path + "\\" + fileName); try { boolean isOkNewFile = newFile.createNewFile(); if (isOkNewFile) { System.out.println("文件创建成功"); } else { System.out.println("文件创建失败"); } } catch (Exception e) { e.printStackTrace(); } } } else { bool = false; File newFile = new File(path + "\\" + fileName); try { boolean isOkNewFile = newFile.createNewFile(); if (isOkNewFile) { System.out.println("文件创建成功"); } else { System.out.println("文件创建失败"); } } catch (Exception e) { e.printStackTrace(); } } }
##File System Configurations#本地工作根目录,注意如果是路径,需要注意转义符号的使用##Local working directionary,becarefull about the path ,int the configuration need "\" noteLOCAL_WORKING_DICTIONARY = D:\\Applications\\BPOES_DATA\\BPOES_BPOFRONT_C002ZRRS\\WORKING_STATION#异常XML备份文件夹名称##Exception XML files backup in this fileEXCEPTION_FILE_BACKUP = XML_BACKUP#报文临时存放目录#Exception XML files ware stored in this files temporaryCUST_XML_TEMP_FILE = CUST_XML_TEMP#报文等待下载图片的目录#XML file in this file, waiting for download the images.CUST_XML_FILE = CUST_XML########################################################################################Db Configurations#Versions Configurations########################################################################################其他属性设置#线程运行间隔时间/毫秒#Thread runing timesTHREAD_TEMP_TIME = 500#XML文件在临时文件夹中存放的至少时间/毫秒#The time of xml stored in fileXML_LESS_STORED_TIME = 120000##配置日志文件信息##The runing log configurations#Information logLOG_INFOS_FILE_PATH = .\\log\\infoLOG_INFOS_FILE_NAME = info.txtLOG_INFOS_FILE_MAXLENGTH = 10#Error logLOG_ERROR_FILE_PATH = .\\log\\errorLOG_ERROR_FILE_NAME = error.txtLOG_ERROR_FILE_MAXLENGTH = 10##The Tempory files infoLOG_TEMP_FILE_NAME = TEMPFILES#######################################################################################IMAGE_STORED = .\\imageIMAGE_NAME = tray2.png#######################################################################################TIME_TYPE = yyyy-MM-dd HH:mm:ss
bool = tempFileTxt.renameTo(newTempFileTxt); //重名名以前可以先判断tempFileTxt是否存在,即判断是否真的复制成功了,//另,临时文件夹没必要每次创建,程序退出时把临时文件夹删除就可以了//还有,为何不直接复制的时候,把目标文件名直接带上日期,这样后面就不需要重命名了 if (bool) { System.out.println(tempFileTxt.getName() + "重命名成功" + bool); } else { System.out.println(tempFileTxt.getName() + "重命名失败" + bool); } // 建立新的文件 boolean bool1 = file.delete(); //如果file文件被其他程序使用,删除必然失败 //所以不应该删除文件,而是把文件内容清空 System.out.println("删除" + file.getPath() + "的结果是" + bool1); System.out.println("开始创建新的文件" + file.getPath()); try { file.createNewFile(); } catch (IOException e) { System.out.println("创建" + file.getName() + "失败" + bool); e.printStackTrace(); }