• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write txt-file, multiple lines  RSS feed

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I would like to save the data I process in a txt-file. I would like to have some information about options, but when I google, I mostly find code for very specific situations.
What I would like is to invoke the writing method many times, each time a new event is the first in list. Each time it is invoked, I would like to add a new line. By doing this, I would like to end up with a nice list, basically looking like this:
eventType, eventTime, employeeID, taskID
eventType, eventTime, employeeID, taskID

I just copied a piece of code from other programmers and altered it slightly. It does not work properly, and I doubt whether this specific method I use is the best for my situation. The biggest problem now is that I have not found how to make a new line every time the method is invoked.
The code:


 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the documentation of BufferedWriter, there's a method in there that does exactly what you need.

Note that you're not closing your file properly. you should be using try-with-resources. You should also use Path instead of File:

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Stephan.

That looks useful. I am a bit confused however about how to convert the different data types to txt.
As you can see, I have a class called Event. I want to write from a list<Event> to a txt-file. The information I want to save for each event is 1 String and 3 int-variables.

How do I make this translation? It seems that the writer is only able to save String-variables. A simple cast does not do the trick.
I am able to write one String-variable (the eventType) to the file, but I am unable to save the int-variables.

Furthermore, I do not know how to make the new-line method work well, as you may well see below. It does provide an enter at both ends of the text, but what I would like to happen is that everytime I invoke this writing method, it writes below the already present data, instead of overwriting it.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Note that you're not closing your file properly. You should be using try-with-resources.

Below that, you comment that it closes automatically. Does that mean I do not need to use try-with-resources?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is a strongly typed language, meaning you can't just cast one type to another.

If your Event object has a good standard text representation, you can override its toString() method. Inside this method, return a string that contains the object's properties. Either that, or you can access the properties from outside the class and build a string then. Whichever, you can use String.format() to do this in a relatively easy way:

In the above format string, each % sign marks a new format specifier. The number before the $ sign indicates which of the arguments to format, in this case 1 for eventType, 2 for eventTime, 3 for employeeID and 4 for taskID.

Each format specifier ends with some letters indicating the conversion to use: 's' to format an object using its string representation, 'd' for integers, 'tF' for date, and 'tT' for time.

Sander Hoovenaar wrote:Below that, you comment that it closes automatically. Does that mean I do not need to use try-with-resources?

No, the syntax I showed you *is* try-with-resources. It automatically closes the resource (in this case the writer) when execution leaves the try-block.

It does provide an enter at both ends of the text, but what I would like to happen is that everytime I invoke this writing method, it writes below the already present data, instead of overwriting it.

Then you need to open the file in append mode:
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was extremely helpful. Is there any way to clear the txt file before writing it?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at StandardOpenOption.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that TRUNCATE_EXISTING does the trick, but this is not possible when using APPEND as well. Is that correct?

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronald Hoovenaar wrote:I would say that TRUNCATE_EXISTING does the trick, but this is not possible when using APPEND as well. Is that correct?



If you want to APPEND then that implies you want to keep the current information and simply add your stuff to the end of it, so TRUNCATING as well is a bit of a contradiction.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!