I have the following method:
public void writeToFile(
String id,
String no,
int systol,
int diastol,
int pulsDiff,
double speedAvg,
double spread
) {
String line =
id + "\t" + no + "\t" + systol + "/" + diastol + "\t" + pulsDiff
+ "\t" + speedAvg + "\t" + spread + "\r\n";
File fil = new File("resultat.txt");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fil, true));
writer.write(line);
writer.close();
} catch (Exception e) {
System.err.println("Fejl: " + e.getMessage());
}
Could this have been done in different ways? With some different methods? And what are the advantages by doing so?
Im trying to understand the advantage of having a bufferedWriter
. I'm still at the beginning and I'm trying to understand this sentence:"
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("foo.out")));
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
Especially the "In general, a Writer sends its output immediately to the underlying character or byte stream" part.