• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing variable values to a file  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a java program which has many lines where I print out variable values to a console
they look like this......................

myConsole.getOut().println("DOW: " + day_of_week2 + ", Hour: " + hour2);

I wish to change my approach and write the variable values to a file now...

I am struggling to understand the necessary code changes to be made

At the moment my thinking is to add the following lines as follows:-



Does this look reasonable?

Bob M  














 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few typos in there. The following code does what I think you want to do i.e. create a file and write to it as required:

With an associated class of:
When run you get:

DOW: Tuesday, Hour: 10:00
DOW: Friday, Hour: 11:00

Good luck,

PaulC.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

Thank you so much for taking the time to reply.............

I shall make the necessary changes

Just one further question

How easy would it be to make the file name dynamic i.e. representative of a variable or two such as date, time etc?

Bob M
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Matthews wrote:How easy would it be to make the file name dynamic i.e. representative of a variable or two such as date, time etc?
Very easy:

Gives a file called Tuesdy.txt

Cheers,

PaulC.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File is of course called Tuesday.txt
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to use a buffered writer for all the writing to the file.
Actually I would probably use a Formatter, taking that file writer as a constructor argument. The true means you are appending to the file, which you doubtless already know.
Your first bit of code is confusing because you have a class with statements outside it. Statements can only be inside methods, constructors, etc.
I would also use try‑with‑resources to close the writer. Otherwise you shou‍ld really write a finally to make sure the writer is closed whether there is an Exception or not.
Let us imagine you have a List<String> which I am calling lines as a source of Strings:-I have never tried this with Formatter, but I think this is how it will work. Note the similarity to the previous:-I believe the Formatter consumes any exceptions so you only need to catch file not found. The %n bit adds the line end appropriate to your current system. Please check the spellings for the buffered writer method carefully in case I have made a mistake. The try‑with‑resources obviates the need to close the writers.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your goal to learn how to do file I/O and all that stuff you're doing with streams and writers or do you just want to save your variables to a file? If it's the latter, then why bother yourself with all those details? I would just use a logger and configure it to write to a file. That or maybe a Properties object, which also has a way to output itself to a file (see https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#list-java.io.PrintWriter-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!