如何修复Word中的文本文件的Wordpad和Notepad格式差异?

时间:2015-04-18 05:49:01

标签: java format string-formatting notepad wordpad

我使用Java生成文件,但在不同的查看器中打开时,我无法正确获取格式。提供不同输出的程序是WordPadNotePad

控制台中程序的输出格式正确。此外,当我在WordPad中查看文件时,它的格式也很好。但是当我在NotePad中打开文件时,它们都混乱了。

在NotePad中看起来非常糟糕,我讨厌最终用户在NotePad中打开它看到乱七八糟的混乱。

有没有办法在Java中正确格式化文本,这样任何文本阅读器都会像我想要的那样呈现它?

我正在使用String.format()格式化我的字符串。

编辑: 以下是我用来写出信息的一些核心代码。

private void retrieveOrderInfo(ArrayList<String> order) {
    int row = orderTable.getRowCount();
    String descr[] = new String[row];
    double price[] = new double[row];
    String info;
    readOrderTable(descr, price);
    for (int i = 0; i < row; i++) {
        info = String.format(" %-45s %-20.1f ", descr[i], price[i]);
        order.add(info);
    }
}

private void writeString(String path, String info, boolean line) {
    DataRW write = new DataRW();
    write.writeFileExternal(path, info, line);
}

这是我的DataRW课程。

public void writeFileExternal(String fileName, String info, boolean line) {
    BufferedWriter bwriter = null;
    try {
        FileWriter fwriter = new FileWriter(fileName, true);
        bwriter = new BufferedWriter(fwriter);
        bwriter.write(info);
        if (line) {
            bwriter.newLine();
        }
        bwriter.flush();
    } catch (IOException e) {
        //e.printStackTrace();
        //System.out.println("Could note write to " + fileName);
        errorMessageNotify("writeexternalfile", fileName);
    } finally {
        if (bwriter != null) {
            try {
                bwriter.close();
            } catch (IOException e) {
                //Just ignore if couldnt close
            }
        }
    }
}

所以我得到了这些信息,并用这些方法调用写出来。

编辑:图像链接 http://i1248.photobucket.com/albums/hh489/EberronBruce/NotepadDemo_zpsangaprji.jpg

http://i1248.photobucket.com/albums/hh489/EberronBruce/wordpad_zpscbu0adkb.jpg

以下是图片的链接,用于描述我的意思。我没有足够的声誉点来直接发布图像。

我真的非常感谢所有的帮助。我一直在敲打这个问题很长一段时间。我尝试使用\ t并调整格式以使其看起来正确,但是一旦我在NotePad下看起来它就会在Wordpad,MS Word和FireFox中查找。

1 个答案:

答案 0 :(得分:1)

我猜测生成的代码使用UNIX换行符。 UNIX换行符时,Wordpad中断。记事本没有。

如果您想要为您的操作系统提供正确的中断,请使用System.getProperty("line.separator")

编辑: 现在我们有截图,我可以说你的问题是记事本。将字体更改为固定宽度字体(如Courier),您的数据将正确对齐。