last day (14 days later) » 

9:31 AM
0
Q: Writing to file in java - close or flush

Idon89I'm writing a logger to my java program (in csv format) with bufferedWriter and FileWriter. When i open the csv file while the program is running and continues writing to the file, I got this exception: "The process cannot access the file because it is being used by another process". What i wa...

 
Please show a full Minimal, Complete, and Verifiable example; you are missing a lot of the important aspects. And for starters; I guess you are creating your BufferedWriter using the FileWriter; so why on earth do you call methods on both of those (you only need to flush/close the outer writer!)
 
@GhostCat - I Added.
 
Should be closed as DUP to stackoverflow.com/questions/16584777/… ... and the answer is spot on: this code is really ... sorry, bad.
 
@GhostCat - Why bad? I just creating a file and writing to him. What could I do better? I just want to take care with the problem that someone open the file while the program is still running...
 
thing is: we could talk about your code for 30 or 60 minutes, there are many problems with it. But I dont even have an answer here which you could upvote or accept ...
 
9:31 AM
@GhostCat - Ok, Thanks.
 
I am trying to get a chat room setup for us. If you want to, we might talk about your code there. I charge 1 upvote for 10 minutes ;-) ( of course, just kidding ;-) ... will get back to you later ... meeting now.
Here we go ... a chat to work with
 
Hi ! thanks for your help
First, i need to apology for my english, shes not that good.
 
9:49 AM
let me know when youre coming back.
 
10:26 AM
I will be out for lunch soon; but lets put down some initial thoughts:
A) conceptually: the other answer is spot on ... although "logging" seems simply on the surface, it is an important and **hard** task to a certain degree. You can make a lot of subtle errors. So if really want to do logging in some "more important" project, consider using some existing library for that. There is no need to re-invent the wheel; there are plenty existing ones (well working, well tested) to pick from
B) Details
And then: be consistent. Either your writer is
A) a FIELD of your class. Then make that field final, and initialize it in your constructor.
In that case, it should simply work to WRITE a message and then FLUSH the writer
B) a local variable. Then create a new Writer in your write method, write, close
In other words: your current code is mixing TWO different ideas, and therefore you ran into your problem
You were creating a NEW writer for each write, but you never closed that writer (flushing does only push stuff out, but it doesn't CLOSE the stream)
I recommend solution A); and when you do that in a proper way, then you should see the updates while your application is running
I hope that is good enough for starters; feel free to ask more questions.
About compensation ... if you are willing to
1) please turn to my profile and go to "view all questions"
2) sort by "votes"
3) you will find 4 questions that have negative vote counts
For each one: look into it; and if you think: "that should not have been downvoted"; feel free to add an upvote. if you think: "this downvote is deserved", then please dont do anything ;-)
I think I also have some "self-answers" to my questions; if you like those answers, again, consider if you think them worth upvoting
On your code again: class names start uppercase, so it should be class Logger.
Beyond that: you should really spent some time at the help center. The code that you put up wont even compile; and it really shows not much of "diligence". One boring, but core skill of programmers: you are disciplined. You dont create waste, you dont keep it around; instead you always strive to come up with a minimal, but complete piece of code, that does exactly what you want it do; and most important: where each line of code makes sense; and is required to have there.
 
Thank you so much, You are awesome! I upvote your answers. And later i will read all what you wrote for me. Thanks again.
 
The other thing: dont put the same question again; when you already got good answer. Instead: invest the time it takes to really digest those answers.
glad to hear that. thanks for your feedback.
and good luck on your further path in programming!
 
 
3 hours later…
1:22 PM
I tried option A, I create a final BufferedWriter and initial him with in the constructor with: bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile(), true));
After that i wrote a lot of messages by bw.append("msg"), And its working fine.
When i open the csv file while the program running, Even that the file opened as a read-mode-only by excel, The program crashed because: java.io.IOException: The process cannot access the file because another process has locked a portion of the file.
 

last day (14 days later) »