• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Write txt-file, multiple lines

 
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15524
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at StandardOpenOption.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic