Java 打印和写入文件时的不同数据

Java 打印和写入文件时的不同数据,java,file-io,Java,File Io,有一个从服务器发送的数据流。我需要将这个字节流存储到一个文件中。问题是我输出到控制台的数据和我存储在文件中的数据是不同的。当我存储在文件中时,数据的格式似乎发生了变化 节目如下: try { System.out.println("My Address is "+serverSocket.getLocalSocketAddress()); Socket server = serverSocket.accept(); // return a new socket

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

节目如下:

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.println(“我的地址是”+serverSocket.getLocalSocketAddress());
Socket server=serverSocket.accept();//返回一个新套接字
System.out.println(“连接到客户端”+server.getRemoteSocketAddress());
inputStream=server.getInputStream();
in=新数据输入流(输入流);
out=newfileoutputstream(“output.txt”);
ArrayList字节=新的ArrayList();
居里;
字节cur;
字节[]限制字节=null;
整数长度=0;
System.out.println(“Before-while循环”);
while((curi=in.read())!=-1&&count!=500)
{
System.out.println(计数+读取一些数据);
//写出(居里);
cur=(字节)curi;
字节。添加(cur);
curBytes=getPrimativeArray(字节);
字符串限制字节字符串=新字符串(限制字节,“UTF-8”);
计数++;
}
int i=0;
for(字节b:字节)
{
系统输出打印(b+“”);
路缘字节[i]=b;
i++;
}
输出。写入(限制字节);
server.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
使用System.out.print(b+“”)打印的内容和我存储在CORBYTES[]中的是同一件事。但是当我比较控制台和文件输出时,它们是不同的

控制台输出:0 113-100-126-54 0 32 14 1 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 19 85 74-81-48 0 13 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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-28127838047504648478467803291504848495853485458524848584858585048484858555658494993584955555548591141019910110511810061504849

文件输出:控制台(
System.out
)是一个
PrintWriter
,而文件输出是一个
FileOutputStream

Stream
Writer
之间的基本区别是:Streams应该操作“原始数据”,就像直接从二进制格式获取的数字一样,而Writer则被用来操作“人类可读数据”,转换您编写的所有数据

例如,
6
int与
6
字符不同。使用流时,直接写入int,而使用writer时,数据writen将转换为字符

然后,如果希望文件输出与控制台输出相同,请不要使用
FileOutputStream
,而是使用,及其方法

如何实现此功能:

1-replace
out=newfileoutputstream(“output.txt”)
by
out=newfilewriter(“output.txt”)

2-替换
out.write(限制字节)作者:

for (byte b : curBytes) {
  out.write(b + " ");
}
我建议您使用和使用 包装您的输入流
输出流显然应该是


我希望这有帮助

这里的其他答案告诉您使用
打印编写器
文件编写器
而不是
文件输出流
,但我相当肯定这不是您想要的

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

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

当您使用
System.out.print
时,您将使用
65
的整数值进行调用,这将依次向终端打印字符
6
5

当您使用
out.write
时,您将调用该函数,将位
01000001
写入文件

稍后,当您检查文件内容时,您的工具将尝试将该字节解释为字符,并且它很可能会使用来执行此操作(即使您使用Unicode作为默认encodi)
$ 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 *
out.close();
server.close();
import java.io.*;
public class Test {
public static void main(String [] args) {

    // The name of the file to create.
    String fileName = "temp.txt";

    try {
        // Put some bytes in a buffer so we can
        // write them. Usually this would be
        // image data or something. Or it might
        // be unicode text.
        String bytes = "Hello theren";
        byte[] buffer = bytes.getBytes();

        FileOutputStream outputStream =
            new FileOutputStream(fileName);

        // write() writes as many bytes from the buffer
        // as the length of the buffer. You can also
        // use
        // write(buffer, offset, length)
        // if you want to write a specific number of
        // bytes, or only part of the buffer.
        outputStream.write(buffer);

        // Always close files.
        outputStream.close();       

        System.out.println("Wrote " + buffer.length + 
            " bytes");
    }
    catch(IOException ex) {
        System.out.println(
            "Error writing file '"
            + fileName + "'");
        // Or we could just do this:
        // ex.printStackTrace();
    }
}
}