免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1182 | 回复: 0
打印 上一主题 下一主题

回复:《再次求助,文件读写操作!!!!(急!急!!)》 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-30 13:18 |只看该作者 |倒序浏览
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//需求:
//现有一临时文件,该文件不断的有新的文本数据添加到文件末尾,每当有新的数据录入时,都会用时间变量与
//统时间进行比较,若时间已过当天,将临时文件改名为以当天日期为文件名的日志文件,并创建新的临时文件,
//录入新的数据。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////




/*
* @(#)LogFile.java
*  
*/

import java.io.*;
import java.util.*;
import java.text.*;

/**
*@author zhenerbaobei
*/

public class LogFile
{
        private Date currentDate = null;
        private Date fileModifyDate = null;
       
       
        public static void main(String[] args)
        {
                LogFile lf = new LogFile("Mon Dec 26 14:35:55 CST 2005:");
        }
       
        public LogFile(String str)
        {
                writeTempFile(str);
        }
       
        /**
         *向临时文件的末尾插入数据
         */
        private void writeTempFile(String str)
        {
                try
                {
                        File tempFile = new File("temp.txt");
                    
                    //判断临时文件是否存在
                    if(tempFile.exists() == false)
                    {
                           error("临时文件不存在");
                    }
                    
                    //判断临时文件是否可写
                    else if(tempFile.canWrite() == false)
                    {
                           error("临时文件不可写");
                    }
               
                    //得到临时文件最后修改日期
                    fileModifyDate = getFileModifyDate();
                    
                    //临时文件最后修改日期与系统日期作比较
                    boolean bool = compareDate(fileModifyDate);
               
                    if(bool == false)
                    {
                              moveTempFile();
                            createNewTemp();
                            
                            //创建文件写入器对象
                            FileWriter filewriter = new FileWriter(tempFile,true);
                            BufferedWriter bufferedWriter = new BufferedWriter(filewriter);
                           
                            //在临时文件末尾追加数据
                        bufferedWriter.write(str);
                        
                        //文件内容换行
                        bufferedWriter.newLine();
                        
                        //关闭BufferedWriter流
                        bufferedWriter.close();
                    }
                    else
                    {
                            //创建文件写入器对象
                            FileWriter filewriter = new FileWriter(tempFile,true);
                            BufferedWriter bufferedWriter = new BufferedWriter(filewriter);
                           
                            //在临时文件末尾追加数据
                        bufferedWriter.write(str);
                        
                        //文件内容换行
                        bufferedWriter.newLine();
                        
                        //关闭BufferedWriter流
                        bufferedWriter.close();
                    }
                }
                //异常捕获
                catch(IllegalArgumentException e)
                {
                        System.out.println(e.getMessage());
                }
                catch(IOException e)
                {
                        System.out.println(e.getMessage());
                }
        }
       
        /**
         *判断日期是否已过当天
         */
        private boolean compareDate(Date fileModifyDate)
        {
                boolean bool = false;
               
                //创建格里高利日历对象
                Calendar currentCalendar = new GregorianCalendar();
                Calendar fileModifyCalendar = new GregorianCalendar();
               
                //设置临时文件修改日期
                fileModifyCalendar.setTime(fileModifyDate);
               
                //得到当前日
                int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
               
                //得到修改日
                int fileModifyDay = fileModifyCalendar.get(Calendar.DAY_OF_MONTH);
               
                if(fileModifyDay < currentDay)
                {
                        bool = false;
                }
                else if(fileModifyDay == currentDay)
                {
                        bool = true;
                }
            
            return bool;
        }
       
        /**
         *得到临时文件的最后修改日期
         */
        private Date getFileModifyDate()
        {
                //创建临时文件对象
                File file = new File("temp.txt");
               
                fileModifyDate = new Date();
               
                //得到临时文件最后修改时间
                long time =file.lastModified();
                fileModifyDate.setTime(time);
               
                return fileModifyDate;
        }
       
        /**
         *将临时文件改名为以日期为文件名的日志文件
         */
        private void moveTempFile() throws IOException
        {
            fileModifyDate = this.getFileModifyDate();
                boolean bool = compareDate(fileModifyDate);
               
               
                Calendar fileModifyCalendar = new GregorianCalendar();
                fileModifyCalendar.setTime(fileModifyDate);
               
                int year = fileModifyCalendar.get(Calendar.YEAR);
                int month = fileModifyCalendar.get(Calendar.MONTH) + 1;
                int day = fileModifyCalendar.get(Calendar.DAY_OF_MONTH);
               
               
                String currentYMDStr = year + "-" + month + "-" + day ;
               
                if(bool == false)
                {
                        File file = new File("temp.txt");
                       
                        //临时文件改名为一日期为文件名的日志文件
                        file.renameTo(new File(currentYMDStr + ".log"));
                       
                        createNewTemp();
                }
        }
       
        /**
         *创建新的临时文件
         */
        private void createNewTemp() throws IOException
        {
          
                  File file = new File("temp.txt");
               
                  if(file.exists() == false)
                  {
                        file.createNewFile();
                        file = new File("temp.txt");
                  }
        }
       
        /**
         *抛出异常的方法
         */
        private static void error(String errorMessage) throws IllegalArgumentException
        {
                throw new IllegalArgumentException(errorMessage);
        }
}

[ 本帖最后由 zhenerbaobei 于 2005-12-30 13:30 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP