簡體   English   中英

java文件閱讀器奇怪的輸出

[英]java file reader weird output

我設置了FileReader,並打開了一個文件進行讀取,但是它給了我一個奇怪的輸出,我似乎無法解決:

import java.io.BufferedReader;
import java.io.FileReader;

public class FileReading {
    public static void main(String [] args) throws Exception {

    FileReader file = new FileReader("/Users/danielpersonius/Desktop/test.rtf");

    BufferedReader reader = new BufferedReader(file);

    String text = "";
    String line = reader.readLine();

    while (line != null){
        // So here, we want to print until it reaches 'null'
        text += line;
        line = reader.readLine();
    }

    System.out.println(text);
}
}

這是我的輸出:

{\\ rtf1 \\ ansi \\ ansicpg1252 \\ cocoartf1265 \\ cocoasubrtf200 {\\ fonttbl \\ f0 \\ fswiss \\ fcharset0 Helvetica;} {\\ colortbl; \\ red255 \\ green255 \\ blue255;} \\ margl1440 \\ margr1440 \\ vieww10800 \\ viewh8400 \\ viewkind0 \\ par tx1440 \\ tx2160 \\ tx2880 \\ tx3600 \\ tx4320 \\ tx5040 \\ tx5760 \\ tx6480 \\ tx7200 \\ tx7920 \\ tx8640 \\ pardirnatural \\ f0 \\ fs24 \\ cf0測試}

TEST是rtf文件所說的,但是我如何擺脫我顯然不想要的所有其他內容? 我正在使用OS X Mavericks的IMac

問題是您可能正在TextEdit中創建文件。 TextEdit不會將文件另存為原始文本文件。 而是將其保存為嵌入格式命令的RTF(富文本文件)格式。 您需要使用可以創建ASCII文本文件的文本編輯器。

有關RTF的更多信息。

只需使用用於編寫代碼的同一編輯器即可創建“ test。*”文件:)

在測試文件上,轉到工具欄中的“格式化”,然后單擊“轉換為.txt文件”以進行轉換。

此更改將擺脫奇怪的輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM