【问题标题】:formatting while writing a document编写文档时格式化
【发布时间】:2016-07-16 11:58:55
【问题描述】:

我正在将一个 txt 文件读入一个字符串缓冲区并使用 OutputStreamWriter 将内容写入一个 word 文档。

问题是文档中没有保留格式。空格和换行符不会像在文本文件中那样保留。 txt 文件的格式正确,包含空格、分页符和制表符。我想在word文档中复制txt。请建议如何保留相同的格式。该文件的链接是:http://s000.tinyupload.com/index.php?file_id=09876662859146558533

这是示例代码:

private static String readTextFile() {
    BufferedReader br = null;
    String content = null;
    try {
        br = new BufferedReader(new FileReader("ORDER_INVOICE.TXT"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            line = br.readLine(); 
            sb.append(System.lineSeparator());
        }
        content = sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return content;
}

private static void createDocument(String docName, String content) {
    FileOutputStream fout = null;
    try {
        fout = new FileOutputStream(docName);
        OutputStreamWriter out = new OutputStreamWriter(fout);
        out.write(content);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

【问题讨论】:

  • OutputStreamWriter 是如何写入“Word”文档的?您是在使用创建 Microsoft Word 文档的库,还是只是为文件提供 .DOC 扩展名?
  • 首先,您正在将所有行读入 StringBuilder,但没有重新添加换行符。 readLine() 返回不带回车或换行符的字符串。所以试试 sb.append(line);其次是 sb.append('\n');
  • public String readLine() throws IOException 读取一行文本。一行被认为是由换行符 ('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。返回:包含行内容的字符串,不包括任何行终止字符,如果已到达流的末尾,则返回 null
  • 线条仍然不同。该行进一步分成不同的行,内容转到下一行,这是文本文件中的单行。

标签: java bufferedreader filewriter


【解决方案1】:

尝试像这样更改您的 readTextFile() 并尝试。

    BufferedReader br = null;
    String content = null;
    try {
        br = new BufferedReader(new FileReader("ORDER_INVOICE.TXT"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while(line != null) {
          content += line + "\n";
          line = br.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return content;

实际上,如果您使用的是 java 7,您可以使用 try-with-resources 来减少代码中的行数。

【讨论】:

  • 其实我在读完这条线后已经在做 sb.append(System.lineSeparator()) 了。问题在于 word 文档中的行转到下一行。我希望这些行的格式与文本文件中的格式相同。
  • 你的男女同校没有表现出你所说的事情。 sb.append(System.lineSeparator())
【解决方案2】:

尽量避免打印 \n 字符。对 Windows 使用 \r\n - 请记住,不同平台的行分隔符不同。

更可靠的方法是使用 PrintWriter,请参阅 How to write new line in Java FileOutputStream

在cmets讨论后:

  • 源文件有unix换行符
  • 输出文件应有 Windows 换行符
  • 我们将从源文件中删除 0x0c(换页 - 即移至打印机的下一页),因为它是不可打印的。

    public static void main(String[] args) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get("f:\\order_invoice.txt")))
            .replace("\u000c","");
    
        PrintWriter printWriter=new PrintWriter(new FileWriter("f:\\new_order_invoice.txt"));
    
        for (String line:content.split("\\n")) {
            printWriter.println(line);
        }
    
        printWriter.close();
    }
    

所以:

  • 将文件原样读取到字符串中
  • 摆脱换页(0x0c,unicode u000c)
  • 在 unix 换行符处拆分字符串 \n
  • 使用PrintWriter逐行写出,它使用平台默认行尾,即windows cr-lf。

请记住,您实际上可以在一行中执行此操作,使用正则表达式将代表整个文件的字符串中的 unix 行结尾替换为 windows 行结尾,并使用 Files.write 在一行中写出整个文件。然而,这个提出的解决方案可能更好,因为它总是使用平台原生行分隔符。

【讨论】:

  • 我没有使用 \n 字符。
  • 对不起,我的错误滚动:) 使用 PrintWriter,并组合一个简单的自容器示例,例如tutorials.jenkov.com/java-io/printwriter.html 使用它的 println 方法来编写带有平台原生换行符的行。它有效:)
  • 我认为问题是如果我比较文本文件中内容之前的空格,它在文本垫中为 62 个字节,在 word 文档中也显示 62 个空格,但内容转到下一行。
  • 好吧,如果您与我们分享您的文本文件,我们一定可以提供更好的帮助。否则我们都只是在黑暗中拍摄:)
猜你喜欢
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
相关资源
最近更新 更多