Can we open a file in constructor of a class with BufferedWriter and write in any other function of same class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we open a file in constructor of a class with BufferedWriter and write in any other function of same class?

open a file once in class as object cretaed and write it with the various function of that class

28th Oct 2016, 5:01 AM
lokesh
lokesh - avatar
1 Answer
+ 3
Of course you can do it. But you have to make sure to close the writer when operation is finished. Create a close method for example which closes the writer and sets its reference to null. From where you use this class you have to call this close method too. To make it safer call flush often when you can.
1st Nov 2016, 10:41 AM
Tamás Barta
Tamás Barta - avatar