简体   繁体   English

读取文本文件会产生意外结果

[英]Reading a text file gives unexpected results

I have a text file with the following contents: 我有一个包含以下内容的文本文件:

23,11,12,16.5
24,23,89,111
12,23,45,89.22

I am reading it with the following code: 我正在阅读以下代码:

do {
    line = fin.readLine();
    System.out.println("dfg   " + line);
    line = fin.readLine();
    System.out.println("dfg   " + line);
} while (line != null);

However, I get unexpected output: 但是,我得到了意外的输出:

dfg   {\rtf1\ansi\ansicpg1251\cocoartf1038\cocoasubrtf320
dfg   {\fonttbl\f0\fmodern\fcharset0 Courier;}

How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?

You're trying to read an RTF (Rich Text file) file as a text file. 您正在尝试将RTF(富文本文件)文件读取为文本文件。

Solution: Don't do that. 解决方案:不要那样做。 Make sure that the file is first saved as a text (txt) file before trying to read it with Java, either that or use an RTF reader (I think that some libraries for this exist, but you would have to download them and use them with your program). 确保先将文件另存为文本(txt)文件,然后再尝试使用Java或使用RTF阅读器将其保存(我认为存在一些与此相关的库,但是您必须下载并使用它们与您的程序)。 Is the file from WordPad? 文件来自写字板吗? If so, be sure to save its content as text. 如果是这样,请确保将其内容另存为文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM