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

时间:2022-07-18 20:29:38

What is the right way to print .dat on Java Console?

在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{  }
}

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

当我原始写入这个dat文件时,我使用

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

3 个解决方案

#1


0  

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.

如果这是您正在编写和阅读的文本,那么您应该使用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();

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);
}

#2


0  

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);

#3


0  

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.

如果控制台上的字符大小不同,则控制台本身配置为显示带有比例字体的文本。这必须在控制台上进行调整,无法通过Java应用程序进行控制。

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)

您正在写入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());

#1


0  

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.

如果这是您正在编写和阅读的文本,那么您应该使用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();

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);
}

#2


0  

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);

#3


0  

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.

如果控制台上的字符大小不同,则控制台本身配置为显示带有比例字体的文本。这必须在控制台上进行调整,无法通过Java应用程序进行控制。

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)

您正在写入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());