[英]Java Formatter class toString() doesn't work
我正在讀一本Java書,現在是將Formatter類輸出到文本文件中。 我在addRecords()
的末尾添加了一行額外的代碼來測試toString()
,但是它不起作用。 為什么會這樣呢?
此外,我對if (output != null)
然后關閉文件時的closeFile()
感到有些困惑。 據我了解,我認為output
已將所有輸入格式化為文本文件,但它仍然不是null,這導致我嘗試在addRecords()
處使用toString()
addRecords()
。
提前致謝!
// 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
這是命令行輸出:
輸入帳號,名字,姓氏和余額。
輸入文件結束指示符以結束輸入。
? 100鮑勃·藍24.98 java.io.BufferedWriter@55f96302
BufferedWriter使用Object的默認toString()實現。 返回的String包含類名稱和Objects hashCode() 。
如果要包含輸出的字符串,請使用String.format或StringWriter格式化輸出,然后再將其寫入文件。
編輯:如其他答案所述,當使用文件名創建格式化程序時,格式化程序將在內部使用BufferedWriter。 在這種情況下, Formatter.toString()調用BufferedWriter.toString()。
我在
addRecords()
的末尾添加了一行額外的代碼來測試toString()
,但是它不起作用。 為什么會這樣呢?
這是工作。 但是它並沒有執行您顯然認為/應該做的事情。
Formatter.toString()
的Javadoc說:
“返回在輸出目標上調用
toString()
的結果。”
在這種情況下,您已經創建了一個寫入BufferedWriter
的Formatter
。 當Formatter.toString()
在BufferedWriter
上調用toString()
時,它不會把您寫入該文件的內容退還給您。 相反,它返回您Object.toString()
將返回的內容。 在這里描述。
如果您希望Java應用程序打印出已寫入文件的內容,則需要將其打開,讀取並將內容復制到System.out。 在執行此操作之前,您需要先flush()
或close()
Formatter
。
一個簡單的想法是在應用程序完成后使用文本編輯器或less
命令或類似的命令查看文件。 如果文件為空或比您期望的短,請確保在終止之前您的應用程序始終關閉Formatter
程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.