Reputation: 479
I'm going through a Java book and now onto Formatter class outputting into a text file. I added in an extra line of code to test for the toString()
towards the end of addRecords()
but it does't work. Why is it so?
Furthermore, I am a bit confused about the closeFile()
at if (output != null)
then close the file. From my understanding is that I thought output
has already formatted all the input into the text file how about it's still not null which leads me to try out the toString()
at addRecords()
.
Thanks in advance!
// Fig. 15.3: CreateTextFile.java
// Writing data to a sequential text file with class Formatter.
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CreateTextFile
{
private static Formatter output; // outputs text to a file
public static void main(String[] args)
{
openFile();
addRecords();
closeFile();
}
// open file clients.txt
public static void openFile()
{
try
{
output = new Formatter("clients.text"); // open the file
}
catch (SecurityException securityException)
{
System.err.println("Write permission denied. Terminating.");
System.exit(1); // terminate the program
}
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file. Terminating.");
System.exit(1); // terminate the program
}
}
// add records to file
public static void addRecords()
{
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n? ",
"Enter account number, first name, last name and balance.",
"Enter end-of-file indicator to end input.");
while (input.hasNext()) // loop until end-of-file indicator
{
try
{
// output new record to file; assumes valid input
output.format("%d %s %s %.2f%n", input.nextInt(),
input.next(), input.next(), input.nextDouble());
}
catch (FormatterClosedException formatterClosedException)
{
System.err.println("Error writing to file. Terminating.");
break;
}
catch (NoSuchElementException elementException)
{
System.err.println("Invalid input. Please try again.");
input.nextLine(); // discard input so user can try again
}
System.out.println(output.toString());
System.out.print("? ");
} // end while
} // end method addRecords
// close file
public static void closeFile()
{
if (output != null)
output.close();
}
} // end class CreateTextFile
Here's the command line output:
Enter account number, first name, last name and balance.
Enter end-of-file indicator to end input.
? 100 Bob Blue 24.98 java.io.BufferedWriter@55f96302
Upvotes: 0
Views: 366
Reputation: 15656
BufferedWriter uses the default toString() implementation from Object. The returned String contains the class name and the Objects hashCode().
If you want a string containing the output either use String.format or a StringWriter to format your output before you write it to the file.
Edit: as mentioned by other answers the BufferedWriter is used internally by the Formatter when it is created with a filename. Formatter.toString() calls BufferedWriter.toString() in this case.
Upvotes: 1
Reputation: 719346
I added in an extra line of code to test for the
toString()
towards the end ofaddRecords()
but it doesn't work. Why is it so?
It is working. But it is not doing what you apparently think it does / should do.
The javadoc for Formatter.toString()
says:
"Returns the result of invoking
toString()
on the destination for the output."
In this case, you have created a Formatter
that writes to a BufferedWriter
. When Formatter.toString()
calls toString()
on a BufferedWriter
, it doesn't give you back the stuff that you wrote to the file. Rather, it returns you what Object.toString()
would return. That is described here.
If you want your Java application print out what has been written to the file, you will need to open it, read it and copy the content to System.out. And before you do that, you will need to flush()
or close()
the Formatter
.
A simpler idea would be to look at the file using a text editor or the less
command, or similar ... after the application has completed. If the file is empty or shorter than you expect, make sure that your application always closes the Formatter
before terminating.
Upvotes: 1