编写文档时格式化

时间:2016-03-29 04:48:24

标签: java bufferedreader filewriter

我正在将一个txt文件读入一个String缓冲区,并使用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();
    }

}

2 个答案:

答案 0 :(得分:0)

尝试像这样更改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来减少代码中的行数。

答案 1 :(得分:0)

尽量避免打印\ n字符。使用\ r \ n for Windows - 请记住,行分隔符在不同平台上有所不同。

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

在评论中讨论后:

  • 源文件具有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将整个文件写入一行。然而,这个呈现的解决方案可能更好,因为它总是使用平台本机行分隔符。

enter image description here