13 Replies Latest reply on Mar 24, 2010 6:32 PM by 807580

    File writing to new file on daily basis

    800271
      Dear All,
      I have a program which is running as a service and keep receiving data and putting to database and text file immediately.The problem now is that if I want to write the file on a daily basis meaning that once is a new day I want it to write into a new file with the file name_date. How can I do that ? I have my basis inserting into file working well. Thank you.
        • 1. Re: File writing to new file on daily basis
          PhHein
          So you know how to write to a file. What is your exact problem? Getting the date? Creating the new file?
          • 2. Re: File writing to new file on daily basis
            800271
            Dear Phein,
            Yes I can write to a file. The problem is that when a new day i want it to write to a new text file? So that is my exact problem cross 12.00am then it should write to a new text file? Thank you.
            • 3. Re: File writing to new file on daily basis
              807580
              If this file is a log file...then you should know that some (most?) logging frameworks have utilities to automatically rotate the logs like that.

              If it's something else... it's still not hard to implement.
              • 4. Re: File writing to new file on daily basis
                800271
                Dear Phein,
                No is a system logging file but is my own custom log file. So I doing it based on my writing just a normal text file only. Thank you.
                • 5. Re: File writing to new file on daily basis
                  PhHein
                  So, just in case you do manual logging, you have to check whether a new day has started before you wirte to the file. If so you create a new file, if not use the old one.
                  • 6. Re: File writing to new file on daily basis
                    800271
                    Dear Phein,
                    Exactly there is where I am stuck now? How to do that check where am I going to do the comparison? Where must I keep the current date and next date? Thank you.
                    • 7. Re: File writing to new file on daily basis
                      PhHein
                      You have a current file or file path from which you can extract the date and you can create a new date every time you log. You would need a SimpleDateFormat to format the current date and String.subString() or String.split to extract the date from the current file name/ filepath.
                      • 8. Re: File writing to new file on daily basis
                        807580
                        newbie14 wrote:
                        Dear Phein,
                        No is a system logging file but is my own custom log file. So I doing it based on my writing just a normal text file only. Thank you.
                        You're still better off using an existing logging framework. The ones I've seen all let you use a custom format, log arbitrary things, etc.

                        And they have log file management tools to automatically rotate logs daily. (or other cycles that you may prefer)

                        Look at the java.util.logging package, or the log4j project (and possibly Jakarta Commons Logging which wraps other loggers).

                        [add]
                        Actually not even as heavyweight as a "log file management tool" as I said above. You just configure your log to rotate daily. Easy peasy.

                        Edited by: paulcw on Mar 24, 2010 11:09 AM
                        • 9. Re: File writing to new file on daily basis
                          PhHein
                          @Paul: ++++
                          • 10. Re: File writing to new file on daily basis
                            800271
                            Dear Paul,
                            I dont get you when you said "You just configure your log to rotate daily". How to configure that I am not so clear about it? Thank you.
                            • 11. Re: File writing to new file on daily basis
                              807580
                              It will depend on the framework you use. But basically there will be configuration file, and in it you say something like "for any log with a name that follows a given pattern, use that log file writer called 'foo'" and then "make the log file writer called 'foo' be the kind of writer that rotates every day at midnight, and give it name like this".

                              There's a small learning curve setting one of these frameworks up but it's less effort than writing your own implementation, PLUS they come with useful features like different logging levels and efficient ways to turn logging on and off, PLUS they're widely used and have lots of people exercising the code and fixing bugs.

                              If I were you I'd look at log4j. I believe it's an Apache project. It's well-documented. The one that comes with the JDK is less well-documented and harder to set up in my opinion.
                              • 12. Re: File writing to new file on daily basis
                                800271
                                Dear Paul,
                                My project is a java standalone application will this be suitable for it? Ok as you suggest I will look into the suggestion but for time being I will use the earlier suggestion of looking of the date and then make a new file with a new filename. Thank you.
                                • 13. Re: File writing to new file on daily basis
                                  807580
                                  newbie14 wrote:
                                  Dear Paul,
                                  My project is a java standalone application will this be suitable for it?
                                  Sure. I have written small standalone apps and used the logging frameworks (which are frequently used in large enterprise apps as well) because it made for quick easy logging.
                                  Ok as you suggest I will look into the suggestion but for time being I will use the earlier suggestion of looking of the date and then make a new file with a new filename. Thank you.
                                  You can if you like but it'll be much easier just to use log4j.