在 Java 控制台上打印 .dat 的正确方法是什么?

发布于 2024-10-30 21:23:26 字数 822 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謸气贵蔟 2024-11-06 21:23:26

如果这是您正在编写和读取的文本,那么您应该使用 Writer 和 Reader 类,并用 BufferedWriter 和 BufferedReader 包装来提供行处理。

FileOutputStream outFile =
    new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
BufferedWriter writer = new BufferedWriter(new Writer(outFile));
writer.write(Acc1.toString());
// maybe write a newline??
writer.newLine();

然后使用缓冲读取器将其读回:

FileInputStream inFile =
    new FileInputStream("C:"+File.separatorChar+"transactions.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(inFile));
while (true) {
    String line = reader.readline();
    if (line == null) break;
    System.out.println(line);
}

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.

FileOutputStream outFile =
    new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
BufferedWriter writer = new BufferedWriter(new Writer(outFile));
writer.write(Acc1.toString());
// maybe write a newline??
writer.newLine();

Then to read it back it you using a buffered reader:

FileInputStream inFile =
    new FileInputStream("C:"+File.separatorChar+"transactions.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(inFile));
while (true) {
    String line = reader.readline();
    if (line == null) break;
    System.out.println(line);
}
不必了 2024-11-06 21:23:26
FileInputStream fis = new FileInputStream("C:"+File.separatorChar+"transactions.dat");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String s;
while ((s=br.readline())!=null)
    System.out.println(s);
FileInputStream fis = new FileInputStream("C:"+File.separatorChar+"transactions.dat");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String s;
while ((s=br.readline())!=null)
    System.out.println(s);
乖不如嘢 2024-11-06 21:23:26

如果控制台上的字符大小不同,则控制台本身会配置为以比例字体显示文本。这必须在控制台上进行调整,并且无法从 java 应用程序进行控制。

您正在写入 ObjectOutputStream。这有点奇怪,但您可能对此有要求。要从这样的文件中读取,请使用以下代码(注意:我稍微改变了您的编写算法!不包括流关闭和异常处理)

// writing

FileOutputStream outFile = new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
ObjectOutputStream out = new ObjectOutputStream(outFile);

String s = brokerageAcc1.toString();
out.writeInt(s.length());
System.out.println(s.length());

out.writeChars(s);

// reading

FileInputStream inFile = new FileInputStream("C:"+File.separatorChar+"transactions.dat");
ObjectInputStream in = new ObjectInputStream(inFile);
int length = in.readInt();     // get the number of chars
System.out.println(""+length);
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++)
  result.append(in.readChar());
System.out.println(result.toString());

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)

// writing

FileOutputStream outFile = new FileOutputStream("C:"+File.separatorChar+"transactions.dat");
ObjectOutputStream out = new ObjectOutputStream(outFile);

String s = brokerageAcc1.toString();
out.writeInt(s.length());
System.out.println(s.length());

out.writeChars(s);

// reading

FileInputStream inFile = new FileInputStream("C:"+File.separatorChar+"transactions.dat");
ObjectInputStream in = new ObjectInputStream(inFile);
int length = in.readInt();     // get the number of chars
System.out.println(""+length);
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++)
  result.append(in.readChar());
System.out.println(result.toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文