首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java单线程,频繁操作文件有关问题

2012-01-18 
Java单线程,频繁操作文件问题。Java code//线程调用的方法public static boolean checkLogFileInfos(String

Java单线程,频繁操作文件问题。

Java code
//线程调用的方法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();            }        }    } 


//一些资源文件,也贴出来吧。
XML code
##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


线程调用每休眠500ms调用一次上面的方法。
打印出来的结果:
=========================================
创建临时文件夹.\log\info\TEMPFILES
将文件info.txt转移到TEMPFILES中
开始重命名.\log\info\TEMPFILES\info.txt
info.txt重命名失败false
删除.\log\info\info.txt的结果是false
开始创建新的文件.\log\info\info.txt
删除.\log\info\TEMPFILES
删除.\log\info\TEMPFILES结果是false
删除.\log\info\TEMPFILES\info.txt结果是true
=========================================
程序实现的功能是:
1.使用线程对info.txt文件的大小进行监控。
2.如果info.txt的文件大小大于固定大小(比如程序中设置的大小事12KB),那么将info.txt转移到同级目录中的TEMPFILE文件夹中,然后将TEMPFILE/info.txt重命名为 (当前日期时间+info.txt)的格式到log/info.txt同级目录中,并且删除info.txt,然后新建info.txt文件。如果小于12KB,那么不做任何操作。
3.删除TEMPFILE/info.txt和TEMPFILE文件和文件夹。
4.线程第一次运行能够实现预期的效果,但是线程运行多次过后,当info.txt文件>12kb时输出的信息如上面。
5.如果我没有说清楚,大家可以将上面的代码复制后自己运行下。就知道问的所在了。
6.最主要的是上面的info.txt文件的大小是随时可变的。也就是正常增长。

[解决办法]
主要问题有:
Java code
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();            } 

热点排行