在 Java 控制台上打印 .dat 的正确方法是什么?
在 Java 控制台上打印 .dat 的正确方法是什么?
public void open(){
try {
FileInputStream inFile =
new FileInputStream("C:"+File.separatorChar+"transactions.dat");
ObjectInputStream in = new ObjectInputStream(inFile);
//While (in.readLine!=null){
System.out.print(in.readLine());}
in.close();
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (Exception ex) {
ex.printStackTrace();
} finally{ }
}
这仅给出单行的结果,并且字符具有不同的大小 如何读取所有数据并以可读输出打印到控制台!
当我最初写入这个 dat 文件时,我使用
FileOutputStream outFile =
new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
ObjectOutputStream out = new ObjectOutputStream(outFile);
out.writeChars(brokerageAcc1.toString());
What is the right way to print .dat on Java Console?
public void open(){
try {
FileInputStream inFile =
new FileInputStream("C:"+File.separatorChar+"transactions.dat");
ObjectInputStream in = new ObjectInputStream(inFile);
//While (in.readLine!=null){
System.out.print(in.readLine());}
in.close();
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (Exception ex) {
ex.printStackTrace();
} finally{ }
}
This gives the result of a single line only and characters have different size
How to read all data and print onto console with readable output!
when I original write in this dat file, I use
FileOutputStream outFile =
new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
ObjectOutputStream out = new ObjectOutputStream(outFile);
out.writeChars(brokerageAcc1.toString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这是您正在编写和读取的文本,那么您应该使用 Writer 和 Reader 类,并用 BufferedWriter 和 BufferedReader 包装来提供行处理。
然后使用缓冲读取器将其读回:
If this is text that you are writing and reading then you should be using the Writer and Reader classes, wrapped with a BufferedWriter and BufferedReader to provide line handling.
Then to read it back it you using a buffered reader:
如果控制台上的字符大小不同,则控制台本身会配置为以比例字体显示文本。这必须在控制台上进行调整,并且无法从 java 应用程序进行控制。
您正在写入
ObjectOutputStream
。这有点奇怪,但您可能对此有要求。要从这样的文件中读取,请使用以下代码(注意:我稍微改变了您的编写算法!不包括流关闭和异常处理)If characters have different sizes on your console, then the console itself is configured to display text with a proportional font. This has to be adjusted on the console and can't be controlled from your java application.
You're writing to an
ObjectOutputStream
. This is somewhat strange, but you may have a requirement for this. To read from such a file, use the following code (Note: I slightly changed your writing algorithm! Stream closing and exception handling NOT included)