aspose file tools*
The moose likes I/O and Streams and the fly likes How to append to a text file for a week Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to append to a text file for a week " Watch "How to append to a text file for a week " New topic
Author

How to append to a text file for a week

Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
Hi,
I am writing a Java program which reads messages off of a queue and then formats the data and has to store this data in a text file,whenever my program runs it has to append to the text file already opened for that week and then it has to close this text file at the end of the week say on sunday nite @ 12.00 and reopen a new text file @ 12.01 sunday nite and start writing and appending to that file for that whole week and so on..
Any idea how to do this?
Any help appreciated.
Thanks.
Ann
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
One option might be to name the file the sunday date of that week
e.g. the file would be 20031019.txt for this week
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Just reread your post, you might want monday's date 20031020.txt
if so, change this
cal.add(Calendar.DATE,-cal.get(Calendar.DAY_OF_WEEK)+1);
to this
if(cal.get(Calendar.DAY_OF_WEEK) == 1)cal.add(Calendar.DATE,-7);
cal.add(Calendar.DATE,-cal.get(Calendar.DAY_OF_WEEK)+2); // note change here
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
But where does it append to the same file?I need to append to this text file everytime the queue has messages to write out.namely whenever my program runs..?
or am I missing something.I am sorry,I am a begginer Java programmer.
Thanks for your help.
Ann
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the 'true' part is append
fw = new FileWriter(fileName,true);
run the code, open the file, you'll see hello world.
run the code again, there should be 2 x hello world
(if you have trouble copying the code - copy it into word first, then copy it from there)
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
Hi Michael,
I am kinda lost with my code.I am trying to get msgs off a queue like I mentioned in my last post but I need to write these msgs into a text file as they come as there can be a lot of msgs on the queue for which I do a MqQ.Get() till all the messages are taken off the queue.This code gets fired again when msgs come onto the queue.Now I am attaching part of my code.Please help me with how to append to the text file after every MQ read and where do I put that code in.
Thanks,
Ann
********************************************************
public class MQGet
{
public static void main(String[] args)
{
MQQueueManager MQQueueManager;
boolean forever = true;
try
{
MQQueueManager qMgr = new MQQueueManager(mqManager);
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
MQQueue mqQ = qMgr.accessQueue(mqQname, openOptions);
MQMessage retrievedMessage = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING
| MQC.MQGMO_NO_SYNCPOINT | MQC.MQGMO_ACCEPT_TRUNCATED_MSG;
gmo.waitInterval = 10000;
while (forever)
{
String outString = "";
int msgLen = 0;
retrievedMessage.messageId = MQC.MQMI_NONE;
retrievedMessage.correlationId = MQC.MQCI_NONE;
mqQ.get(retrievedMessage, gmo);
msgLen = retrievedMessage.getMessageLength();
outString = retrievedMessage.readString(msgLen);
System.out.println(msgLen);
if (retrievedMessage.getMessageLength() != 0)
{
String f0 = outString.substring(0,8);
String f1 = outString.substring(8,14);
String f14 = outString.substring(67,(msgLen+1));
System.out.println(f14);
}
else
{
System.out.println(" No Messages available on Queue ");
}
class OpenSundayTextFile
{
FileWriter fw;
public OpenSundayTextFile() throws IOException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.DAY_OF_WEEK) == 1)cal.add(Calendar.DATE,-7);
cal.add(Calendar.DATE,-cal.get(Calendar.DAY_OF_WEEK)+2);
String fileName = sdf.format(cal.getTime()) + ".txt";
fw = new FileWriter(fileName,true);
doSomething();
goodBye();
}
public void doSomething() throws IOException
{
fw.write("hello world\r\n");
}
public void goodBye() throws IOException
{
fw.close();
System.exit(0);
}
//This line gives me an error : syntax error on new//
new OpenSundayTextFile();
}
}
mqQ.close();
qMgr.disconnect();
}
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
I was re-reading my code nad found some copy-paste mistakes,this is the correct code..
if (retrievedMessage.getMessageLength() != 0)
{
String f0 = outString.substring(0,8);
String f1 = outString.substring(8,14);
String f14 = outString.substring(67,(msgLen+1));
//this is a change
String f15 = fo + f1 + f14;
System.out.println(f15);
}
else
{
System.out.println(" No Messages available on Queue ");
}
class OpenSundayTextFile
{
FileWriter fw;
public OpenSundayTextFile() throws IOException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.DAY_OF_WEEK) == 1)cal.add(Calendar.DATE,-7);
cal.add(Calendar.DATE,-cal.get(Calendar.DAY_OF_WEEK)+2);
String fileName = sdf.format(cal.getTime()) + ".txt";
fw = new FileWriter(fileName,true);
doSomething();
goodBye();
}
public void doSomething() throws IOException
{
//this is a change
fw.write("f15\r\n");
}
public void goodBye() throws IOException
{
fw.close();
System.exit(0);
}
//This line gives me an error : syntax error on new//
new OpenSundayTextFile();
}
}
mqQ.close();
qMgr.disconnect();
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'm moving this fancy I/O talk to the IO and Streams forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
Hi,
I dont get this.....!!! Both my threads get closed in both the forums...
one place(I/O) says this is being addressed in another forum so they are closing this thread and in this other forum(Java Begginers),someone says they are moving this thread to (I/O) cuz it is too fancy for begginers forum..Will someone pl help me with this.pl check the other forum for responses before u go ahead and close threads...
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ann, I will explain this to you once again.
You created a new post in I/O asking the EXACT SAME QUESTION that you posted in this thread, which was originally in the Beginners forum.
Since you are posting the same question in multiple places, I closed the extra one in this forum, since Dirk moved this from Beginners to I/O.
We are very aware of what is occuring with the posts, more so than you are apparently. Asking the same question in two places does nothing but give us moderators more to deal with, and trying to tell us how to do our job does nothing but make us less likely to deal with the situation in a friendly manner.
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
I am sorry if u think i am trying to tell you how to do your job.I am not and I dont envy your position,dealing with so many questions..I am not second guessing anyone,if I sounded like that,I didn't mean to.
I know this is a forum running on goodwill and people helping out with what knowledge they r willing to share.I am just running short on time with my deadline and thought I cud post it at 2 places and get a response faster and then no one was responding anywhere so I got confused.
Thanks for your help and sorry for the wrong impression.
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
This is a very typical issue, people knowingly/unknowingly posting their questions in two or more forums. They might have valid reasons but it just means additional work for the moderators to handle and can lead to closing/moving the posts. In such situations when both parties have valid reasons, I think application should handle this situation.
I will suggest that some changes should be done so that a user is allowed to post his/her question under multiple forums but application maintains only one physical instance of the question/post. This way no matter from whichever forum a post is accessed, same details/updates are visible.
Just my 2 cents.
Ashish
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ashish, that is a good suggestion, because one of the reasons we discourage it is for the reason you mention. How about starting that as a suggestion in the Javaranch forum, you are bound to get a good discussion on it. Another reason is that it clutters the database, but having a solution that contains only one instance would negate that.
There is also the reason that the forums are separated into appropriate topics to keep related questions together. Otherwise, and not that Ann would do this (because the cross-posting was logical, one in I/O and one in Beginners), we would end up with people posting the same question in almost ALL the forums. Believe me, we've seen this, and it really creates a mess. So we try to build a culture where people ask a question once in the forum that most suits it.
Sorry if I came off harsh Ann, I just thought we had established why the posts were moved/closed. I'll be the first to admit I don't always "get it" on the first go around.
Ok, enough of that discussion, we'll let the post get back to the original question. Hope you meet your deadline!
[ December 08, 2003: Message edited by: jason adam ]
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
Jason,
Thanks for appreciating my suggestion. I like your idea to start discussion on this in JavaRanch forum but I am not sure if it will be easy to digest the responses from other forum members.

There is also the reason that the forums are separated into appropriate topics to keep related questions together. Otherwise, and not that Ann would do this (because the cross-posting was logical, one in I/O and one in Beginners), we would end up with people posting the same question in almost ALL the forums. Believe me, we've seen this, and it really creates a mess. So we try to build a culture where people ask a question once in the forum that most suits it.

In response to your above concern, I will suggest the following
1. Select the 'forum' from where user starts creating the post.
2. provide option (as checkboxes) to user to select any additional forum(s). By default non other is selected.
3. Pop up a confirmation window for every checkbox checked. This will deter most not to select any unnecessary forums. No confirmation for the forum from where the question is being posted.
4. Again when the user is finally posting the question, pop up the confirmation window for each selected (except the default) forum. This, I guess, will beak the will of those breve hearts who make so far.
5. For those serious and genuine users who really want to post their question to more then one forum, include a friendly reminder with each confirmation. I guess they will understand.
I hope I am able to put forward my views clearly. In case you have any questions, please feel free to post them here.
Ashish
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ashish - as Jason suggested, this would be a good topic for the JavaRanch forum. The one over here. Let's not take over Ann's topic when she's still trying to get I/O questions answered.
OK, so why haven't we answered Ann's followup questions here already? Personally Ann, I (and I suspect others too) found it difficult to read the code, since there's no indentation. Please indent your code according to its structure, and then use [code] tags to preserve that indentation when you post. You can edit your previous posts to fix them - use the pencil-and-paper icon above each post. People will be more likely to respond to posts they can read easily.
Please help me with how to append to the text file after every MQ read and where do I put that code in.
I think maybe you're trying to do too many new things at once here before you understand how they relate. Try concentrating on how to append text to a file. (Forget about your MQGet class for now.) Do you understand how Michael's OpenSundayTextFile code works? Do you have questions about it? Can you run it and see if it works? If you think you understand it, then try modifying the doSomething() method so that instead of writing "f15\r\n" it writes a variable String which you provide as a method parameter. Write a mein() method which calls this new method with several different Strings, and see if they're being logged to the file correctly. Then after you've got the basics of file-writing working, try calling this code from the MQGet class.


"I'm not back." - Bill Harding, Twister
Ann Ron
Greenhorn

Joined: Oct 21, 2003
Posts: 24
Hi Jim,
I did understand what Michael had explained about filewriter and I did pass String variable to doSomething() and it prints in the log just fine.I am still coming across some errors which I think deal with scope issues but am not able to resolve it.I get an error f15 cannot be resolved but I dont know where else to declare it ,cuz f15 is actually calculated in the while block after getting it from the queue.

Thanks a lot for all the patience,I know I have tried quite a few people's.. Ann
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to append to a text file for a week
 
Similar Threads
How to append to a text file
Appending Text to AutoExce.bat
Checking for Date Time
append to a text file at the end of the week
How to reopen a topic for Appending to a textfile.