java - 打印和写入文件时数据不同

标签 java file-io

有一个从服务器发送的数据流。我需要将此字节流存储到文件中。问题是我输出到控制台的数据和我存储在文件中的数据不同。当我存储在文件中时,数据格式似乎发生了变化。

这是程序:

try
{
    System.out.println("My Address is "+serverSocket.getLocalSocketAddress());
    Socket server = serverSocket.accept();      // return a new socket
    System.out.println("Connected to client "+server.getRemoteSocketAddress());
    inputStream = server.getInputStream();
    in = new DataInputStream(inputStream);
    out = new FileOutputStream("output.txt");
    ArrayList<Byte> bytes = new ArrayList<Byte>();

    int curi;
    byte cur;
    byte[] curBytes = null;
    int length = 0;
    System.out.println("Before while loop");
    while((curi = in.read())!=-1 && count!=500)
    {
        System.out.println(count+" Reading some data");
        //out.write(curi);
        cur = (byte)curi;
        bytes.add(cur);
        curBytes = getPrimativeArray(bytes);

        String curBytesString = new String(curBytes, "UTF-8");
        count++;
    }
    int i=0;
    for(byte b : bytes)
    {
        System.out.print(b+"  ");
        curBytes[i] = b;
        i++;
    }
    out.write(curBytes);

    server.close();
}
catch(IOException e)
{
    e.printStackTrace();
}

我使用 System.out.print(b+""); 打印的内容和我存储在 curBytes[] 中的内容是同一件事。但是当我比较控制台和文件输出时,它们是不同的。

控制台输出: 0 0 113 -100 -126 -54 0 32 14 1 0 0 1 -58 60 54 0 3 63 -2 85 74 -81 -88 0 9 1 24 85 74 -81 -48 0 13 65 -113 85 74 -81 -88 0 12 125 -126 85 74 -81 -88 0 13 21 97 85 74 -81 -88 0 13 31 19 85 74 -81 -48 0 13 42 24 0 6 0 0 0 0 0 0 0 0 0 0 32 0 7 -100 0 -5 6 -128 0 -56 29 -127 23 112 -1 -1 0 0 64 0 1 -121 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 49 53 64 111 110 101 46 97 116 116 46 110 101 116 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 54 53 64 111 110 101 46 97 116 116 46 110 101 116 37 50 57 54 53 45 49 53 48 53 48 54 50 51 50 55 48 50 45 50 48 53 48 54 54 50 55 54 54 64 48 48 55 56 48 48 49 49 16 32 1 5 6 64 0 0 0 32 16 0 0 0 120 0 17 16 32 1 24 -112 16 1 46 2 0 0 0 0 0 0 0 6 1 -113 0 4 0 33 -64 -42 0 91 5 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -120 -28 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -44 -39 8 0 4 0 -56 0 0 1 11 3 85 74 -81 -88 0 9 1 24 8 0 5 0 0 0 0 0 0 3 85 74 -81 -88 0 13 31 19 8 0 1 0 -56 0 0 0 6 3 85 74 -81 -48 0 13 42 24 -64 34 4 24 9 89 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 98 114 97 110 99 104 61 122 57 104 71 52 98 75 50 57 48 45 48 48 55 56 48 48 49 49 45 48 48 48 102 45 52 52 49 57 55 49 52 48 51 3 85 74 -81 -88 0 12 -120 -28 127 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 114 101 99 101 105 118 101 100 61 50 48 48 49

文件输出: ^@^@q<9c><82>Ê^@ ^N^A^@^@^AÆ<6^@^C?þUJ¯¨^@ ^A^XUJ¯Ð^@^MA<8f>UJ¯¨^@^L}<82>UJ¯¨^@^M^UaUJ¯¨^@^M^_^SUJ¯Ð^@^M*^X^@^F^@^@^@^@^@^@^@^@^@^@ ^@^G<9c>^@û^F<80>^@È^]<81>^Wpÿÿ^@^@@^@^A<87>^\sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f1dac0c5c3c3c3c1c8c8c8c0c4b19e9f94df908585df9f9485" rel="noreferrer noopener nofollow">[email protected]</a>^\sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94bfa5a0a6a6a6a4adadada2a1d4fbfaf1baf5e0e0bafaf1e0" rel="noreferrer noopener nofollow">[email protected]</a>%2965-150506232702-2050662766@00780011^P ^A^E^F@^@^@^@ ^P^@^@^@x^@^Q^P ^A^X<90>^P^A.^B^@^@^@^@^@^@^@^F^A<8f>^@^D^@!ÀÖ^@[^E^H^@ ^@È^@^@^@^O^CUJ¯¨^@^L<88>ä^H^@ ^@È^@^@^@^O^CUJ¯¨^@^LÔÙ^H^@^D^@È^@^@^A^K^CUJ¯¨^@ ^A^X^H^@^E^@^@^@^@^@^@^CUJ¯¨^@^M^_^S^H^@^A^@È^@^@^@^F^CUJ¯Ð^@^M*^XÀ"^D^X YSIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;branch=z9hG4bK290-00780011-000f-441971403^CUJ¯¨^@^L<88>ä^?SIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;received=2001

请让我知道我在哪一步犯了错误。

最佳答案

这里的其他答案告诉您使用 PrintWriterFileWriter 而不是 FileOutputStream 但我相当确定这是不是你想要的。

您的问题是,您将原始字节写入文件,然后将其作为字符读回,并将其与表示为字符的字节值进行比较,然后使用 System.out 打印。

让我们看看当您打印值为 65(或二进制的 01000001)的字节时会发生什么。

当您使用System.out.print时,您将调用PrintStream.print(int)整数值 65 会依次将字符 65 打印到终端。

当您使用out.write时,您将调用 FileOutputStream.write(byte[])这会将 01000001 位写入文件。

稍后,当您检查文件的内容时,您的工具将尝试将此字节解释为字符,并且很可能会使用 ASCII encoding这样做(即使您使用 Unicode 作为默认编码,这也可能会发生,因为 Unicode 是 ASCII 的超集)。这会导致打印字符 A

如果您想以类似于使用 System.out.print 打印的方式查看输出文件,您可以在 Linux 上使用以下命令:

$ hexdump -e '/1 "%i "' <file>

示例:

$ cat /etc/issue
Ubuntu 12.04.5 LTS \n \l
$ hexdump -e '/1 "%i "' /etc/issue
85 98 117 110 116 117 32 49 50 46 48 52 46 53 32 76 84 83 32 92 110 32
92 108 10 *

关于java - 打印和写入文件时数据不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090073/

相关文章:

java - 在java中使用日志的正确方法?

java - LWJGL3 'working' 演示抛出 'no OpenGL context current in the current thread' 异常

java - 按两个字段分组,然后对 BigDecimal 求和

c - C 中的 open() 和 write() 函数

c# - 如何使用 .NET 快速获取目录中最旧的文件?

java - 如何从空引用访问静态变量?

java - 如何将 Project Explorer View 添加到 Eclipse RCP 应用程序

ios - 使用 iOS 保存文件。他们去哪里?

c++ - 从 txt 文件 [C++] 中只读取用户名

c - 反转文件的 n 个字符