有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java PrintStream Println在不同的机器上表现不同?

我有一个简单的服务器和客户端软件。服务器运行在mac上,并让我的自定义代码用C编写,以模拟socket中的readline

用java为安卓和windows机器编写的客户端。我在Println通话中观察到奇怪的行为。代码如下所示

    PrintStream ps = new PrintStream(clientSocket.getOutputStream());   
    JSONObject json = new JSONObject(responseMap);
    String buffer = json.toString();
    ps.println(buffer);
    ps.flush();

在windows上运行时,println在缓冲区字符串后发送/r/n。 在安卓上运行时,println在缓冲区字符串后发送/n

println的行为是否应在不同的操作系统/机器之间保持一致


共 (2) 个答案

  1. # 1 楼答案

    Should the behaviour of println not be consistent accross diff OS/machines ?

    不,我不知道你是从哪里得到这个主意的。您描述的行为是as documented

    The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').
  2. # 2 楼答案

    Should the behaviour of println not be consistent accross diff OS/machines ?

    PrintStream检索平台的行分隔符并将其发送到底层流

    Windows和Android有不同的行分隔符