根据操作系统

时间:2016-04-20 00:58:38

标签: java linux unix binary ebcdic

我正在运行下面的代码,我得到的结果与#34; some_string" .getBytes()有所不同,这取决于我是在Windows还是Unix。任何字符串都会出现问题(我尝试了一个非常简单的ABC和同样的问题。

请参阅下面在控制台中打印的差异。

下面的代码使用Java 7进行了充分测试。如果您完全复制它,它将运行。

此外,请参阅下面两张图片中十六进制的差异。前两个图像显示在Windows中创建的文件。您可以分别使用ANSI和EBCDIC查看十六进制值。第三个图像是黑色的,来自Unix。你可以看到十六进制(-c选项)和我认为它是EBCDIC的字符可读。

所以,我直截了当的问题是:为什么这样的代码工作不同,因为我在这两种情况下只使用Java 7?我应该在某个地方检查任何具体的财产吗?也许,Windows中的Java获得某种默认格式,而在Unix中则获得另一种格式。如果是这样,我必须检查或安排哪个属性?

enter image description here

Unix控制台:

$ ./java -cp /usr/test.jar test.mainframe.read.test.TestGetBytes
H = 76 - L
< wasn't found

Windows控制台:

H = 60 - <
H1 = 69 - E
H2 = 79 - O
H3 = 77 - M
H4 = 62 - >
End of Message found

整个代码:

package test.mainframe.read.test;

import java.util.ArrayList;

public class TestGetBytes {

       public static void main(String[] args) {
              try {
                     ArrayList ipmMessage = new ArrayList();
                     ipmMessage.add(newLine());

                     //Windows Path
                     writeMessage("C:/temp/test_bytes.ipm", ipmMessage);
                     reformatFile("C:/temp/test_bytes.ipm");
                     //Unix Path
                     //writeMessage("/usr/temp/test_bytes.ipm", ipmMessage);
                     //reformatFile("/usr/temp/test_bytes.ipm");
              } catch (Exception e) {

                     System.out.println(e.getMessage());
              }
       }

       public static byte[] newLine() {
              return "<EOM>".getBytes();
       }

       public static void writeMessage(String fileName, ArrayList ipmMessage)
                     throws java.io.FileNotFoundException, java.io.IOException {

              java.io.DataOutputStream dos = new java.io.DataOutputStream(
                           new java.io.FileOutputStream(fileName, true));
              for (int i = 0; i < ipmMessage.size(); i++) {
                     try {
                           int[] intValues = (int[]) ipmMessage.get(i);
                           for (int j = 0; j < intValues.length; j++) {
                                  dos.write(intValues[j]);
                           }
                     } catch (ClassCastException e) {
                           byte[] byteValues = (byte[]) ipmMessage.get(i);
                           dos.write(byteValues);
                     }
              }
              dos.flush();
              dos.close();

       }

       // reformat to U1014
       public static void reformatFile(String filename)
                     throws java.io.FileNotFoundException, java.io.IOException {
              java.io.FileInputStream fis = new java.io.FileInputStream(filename);
              java.io.DataInputStream br = new java.io.DataInputStream(fis);

              int h = br.read();
              System.out.println("H = " + h + " - " + (char)h);

              if ((char) h == '<') {// Check for <EOM>

                     int h1 = br.read();
                     System.out.println("H1 = " + h1 + " - " + (char)h1);
                     int h2 = br.read();
                     System.out.println("H2 = " + h2 + " - " + (char)h2);
                     int h3 = br.read();
                     System.out.println("H3 = " + h3 + " - " + (char)h3);
                     int h4 = br.read();
                     System.out.println("H4 = " + h4 + " - " + (char)h4);
                     if ((char) h1 == 'E' && (char) h2 == 'O' && (char) h3 == 'M'
                                  && (char) h4 == '>') {
                           System.out.println("End of Message found");
                     }
                     else{
                           System.out.println("EOM not found but < was found");
                     }
              }
              else{
                     System.out.println("< wasn't found");
              }
       }
}

1 个答案:

答案 0 :(得分:3)

调用 guard let fromLoc = self.currentDetailObj!["fromPosition"] as? PFGeoPoint else{ return } 时没有指定字符集,因此它使用底层平台的默认字符集(如果在启动Java时指定,则使用Java本身)。这在String documentation

中说明
  

public byte [] getBytes()

     

使用平台的默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。

getBytes()有一个重载版本,可让您在代码中指定一个字符集。

  

public byte [] getBytes(Charset charset)

     

使用给定的字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。