Java 格式化程序 class toString() 不起作用
Java Formatter class toString() doesn't work
我正在浏览一本 Java 书,现在开始使用 Formatter class 输出到文本文件。我添加了一行额外的代码来测试 toString()
接近 addRecords()
的末尾,但它不起作用。为什么会这样?
此外,我对 if (output != null)
处的 closeFile()
有点困惑,然后关闭文件。据我了解,我认为 output
已经将所有输入格式化为文本文件,但它仍然不为空,这导致我在 addRecords()
.[=20 处尝试 toString()
=]
提前致谢!
// 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 uses the default toString() implementation from Object. The returned String contains the class name and the Objects hashCode().
如果您想要一个包含输出的字符串,请在将输出写入文件之前使用 String.format or a StringWriter 格式化您的输出。
Edit: 如其他答案所述,当使用文件名创建 BufferedWriter 时,格式化程序在内部使用它。 Formatter.toString() 在这种情况下调用 BufferedWriter.toString()。
I added in an extra line of code to test for the toString()
towards the end of addRecords()
but it doesn't work. Why is it so?
正在运行。但它并没有做你显然认为它做/应该做的事情。
Formatter.toString()
的 javadoc 说:
"Returns the result of invoking toString()
on the destination for the output."
在这种情况下,您创建了一个 Formatter
写入 BufferedWriter
。当 Formatter.toString()
在 BufferedWriter
上调用 toString()
时,它不会返回您写入文件的内容。相反,它 return 是你 Object.toString()
会 return 的东西。即描述here.
如果您希望 Java 应用程序打印出已写入文件的内容,您需要打开它,读取它并将内容复制到 System.out。在此之前,您需要 flush()
或 close()
Formatter
.
一个更简单的想法是在应用程序完成后使用文本编辑器或 less
命令或类似...查看文件。如果文件为空或比您预期的要短,请确保您的应用程序 总是 在终止前关闭 Formatter
。
我正在浏览一本 Java 书,现在开始使用 Formatter class 输出到文本文件。我添加了一行额外的代码来测试 toString()
接近 addRecords()
的末尾,但它不起作用。为什么会这样?
此外,我对 if (output != null)
处的 closeFile()
有点困惑,然后关闭文件。据我了解,我认为 output
已经将所有输入格式化为文本文件,但它仍然不为空,这导致我在 addRecords()
.[=20 处尝试 toString()
=]
提前致谢!
// 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 uses the default toString() implementation from Object. The returned String contains the class name and the Objects hashCode().
如果您想要一个包含输出的字符串,请在将输出写入文件之前使用 String.format or a StringWriter 格式化您的输出。
Edit: 如其他答案所述,当使用文件名创建 BufferedWriter 时,格式化程序在内部使用它。 Formatter.toString() 在这种情况下调用 BufferedWriter.toString()。
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?
正在运行。但它并没有做你显然认为它做/应该做的事情。
Formatter.toString()
的 javadoc 说:
"Returns the result of invoking
toString()
on the destination for the output."
在这种情况下,您创建了一个 Formatter
写入 BufferedWriter
。当 Formatter.toString()
在 BufferedWriter
上调用 toString()
时,它不会返回您写入文件的内容。相反,它 return 是你 Object.toString()
会 return 的东西。即描述here.
如果您希望 Java 应用程序打印出已写入文件的内容,您需要打开它,读取它并将内容复制到 System.out。在此之前,您需要 flush()
或 close()
Formatter
.
一个更简单的想法是在应用程序完成后使用文本编辑器或 less
命令或类似...查看文件。如果文件为空或比您预期的要短,请确保您的应用程序 总是 在终止前关闭 Formatter
。