【问题标题】:Writing to file for mac and windows with java使用java写入mac和windows的文件
【发布时间】:2016-03-23 08:13:56
【问题描述】:

在 Windows 中写入文件时出现问题。我的 JAVA 程序在我的 Mac 上运行良好,但在 windows 中就无法运行。

这是在 windows 中执行不同的代码:

String string = textArea.getText();

if (string.contains(System.getProperty("line.separator"))) {
    notatet = string.replace(System.getProperty("line.separator"), "<line.separator>");
}

当我将此字符串保存到 MAC 中的 txt 文件时,我会得到:

Line1<line.separator>Line2<line.separator><line.separator>Line3

但是当我将此字符串保存到 WINDOWS 中的 txt 文件时,我得到了这个:

Line1
Line2

Line3

现在我显然想要 Mac 选项,那是我的目标。我应该如何处理我的代码以使其在两个/所有操作系统中都能正常工作?

【问题讨论】:

  • 旁注:你有来自java 7的System.lineSeparator()docs.oracle.com/javase/7/docs/api/java/lang/…
  • 以上都不是。避免硬编码特殊字符,并使用println()。它知道系统行分隔符是什么。
  • line.separator 在 Windows 上是 \r\n。检查您的文本区域,看看它是否真的包含此分隔符,而不仅仅是 \n
  • @jhamon 我已经检查过了,它确实包含这个分隔符。
  • @ReutSharabani 这可能是因为我的 Windows 上没有更新 java 吗?

标签: java file system separator


【解决方案1】:

问题是您的字符串中可能有\r\n\n 作为行分隔符。您的 if 语句与哪个不匹配。只需使用正则表达式和 replaceAll 方法。

参见以下示例:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String string = "Test\ntest\r\ntest";
        String n = string.replaceAll("[\\r\\n]+", "<line.separator>");
        System.out.print(n);
    }
}

输出: 测试测试测试

Running Example

编辑

对于Test\n\ntest\r\ntest 的情况,上面的代码不能正常工作。 将正则表达式替换为以下行:

    String n = string.replaceAll("\\n", "<line.separator>");
    n = n.replaceAll("\\r", "");

因此,如果出现多个\r\n\n,替换将正常工作。 因为所有案例都包含\n,所以首先将它们替换为&lt;line.separator&gt;,然后n.replaceAll("\\r", ""); 行进行了清理并删除了不必要的\r

【讨论】:

  • 当我测试这个时,我只遇到一个问题。如果我有双 \n 它只会将它检测为一个。如果字符串是"Test\n\ntest\r\ntest",我仍然得到; Test&lt;line.separator&gt;test&lt;line.separator&gt;test.. 不是; Test&lt;line.separator&gt;&lt;line.separator&gt;test&lt;line.separator&gt;test,应该是这个
  • 但是如果我从正则表达式中删除+,它会按照我的意图工作,以检测多个换行符。删除+ 是正确的方法吗?
  • 没有问题是你也得到\r\n 两个行分隔符,这是错误的。但我有另一个解决方案。查看我的编辑答案
  • @Zelldon,您编辑的答案不适用于"Test\rTest" 之类的字符串,只能打印TestTest
  • 是的,因为 \r 不是有效的换行符 \r\n 或 \n 将只是一个有效的行分隔符
【解决方案2】:

有易于使用的Linebreak matcher\R,它可以处理所有各种\n\r\r\n 组合。

String text = "A\r\nB\n\nC\rD\rE\r\rF";
String res = text.replaceAll("\\R", "<line.separator>");
System.out.println(res);

将打印

A<line.separator>B<line.separator><line.separator>C<line.separator>D<line.separator>E<line.separator><line.separator>F

并且应该适用于所有操作系统。

编辑\R 是 Java 8 的新功能。您可以使用 \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029] 用于旧版本。

String res = text.replaceAll("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]",
    "<line.separator>");

【讨论】:

  • 我收到此错误illegal/unsupported escape sequence。在"\\R"。这是为什么呢?
  • Pattern.quote("\\R") 会做同样的工作吗?
  • @Peter,我猜您正在使用 JVM 1.7 或更低版本。 \R 是 1.8 的新版本。我在回复 Zelldon 时发布的正则表达式应该是等效的,并且可以与较旧的 JVM 一起使用
  • 又是\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
  • 所以string = string.replaceAll("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]", "&lt;line.separator&gt;"); 会完全一样吗?
猜你喜欢
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多