简体   繁体   English

Java:从字节到字符的转换以及如何使用dataOutputStream

[英]Java: transformation from byte to char and how to use dataOutputStream

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestEOF {
    // Throw exceptions to console:
    public static void main(String[] args) throws IOException {
        DataInputStream in = new DataInputStream(new BufferedInputStream(
                new FileInputStream("TestEOF.txt")));
        while (in.available() != 0)
            System.out.print((char)(in.readByte()));
        //testDataOutputStream
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
                new FileOutputStream("ABC.txt")));
        out.writeDouble(12.33333333);
        out.writeDouble(82918293.3334893320109);
                out.flush();
        DataInputStream dis = new DataInputStream(new BufferedInputStream(
                new FileInputStream("ABC.txt")));
        while (dis.available() != 0)
            System.out.print(Byte.toString(dis.readByte()));
                          System.out.println(dis.readByte());
    }
} // /:~

Hi, guys.嗨,大家好。 I thought char needs two bytes in java, so "System.out.print((char)(in.readByte()))" cannot work or it should give me wrong answer since every time it converts one byte to a char.我认为 char 在 java 中需要两个字节,所以“System.out.print((char)(in.readByte()))”不能工作,或者它应该给我错误的答案,因为每次它将一个字节转换为一个 char。 However, it works well: Whatever I put in the "TestEOF.txt" file can be printed out correctly.但是,它运行良好:我在“TestEOF.txt”文件中输入的任何内容都可以正确打印出来。

For the part starting from "//testDataOutputStream", Byte.toString(dis.readByte()) works well, but dis.readByte() throws an exception.对于从“//testDataOutputStream”开始的部分,Byte.toString(dis.readByte()) 效果很好,但是dis.readByte() 会抛出异常。 It really confuses me.这真的让我很困惑。

Anyone can help?任何人都可以帮忙吗? Thank you very much.非常感谢。

The output of TestEOF.txt won't work if the file has some multibyte characters in character encodings other than Unicode 16 and ASCII .如果文件在Unicode 16ASCII以外的字符编码中包含一些多字节字符,则 TestEOF.txt 的TestEOF.txt将不起作用。 The casting happened to work because the contents are coincidentally equal to their multibyte counterpart (because they are in ASCII).转换恰好起作用,因为内容巧合地等于它们的多字节对应物(因为它们是 ASCII 格式)。

The last line System.out.println(dis.readByte());最后一行System.out.println(dis.readByte()); throws an EOFException because there is no more content available in the input stream.抛出 EOFException,因为输入 stream 中没有更多可用内容。

EDIT编辑

To retrieve the double s saved in ABC.txt , simply do something like要检索保存在ABC.txt中的double ,只需执行类似的操作

DataInputStream dis = new DataInputStream(new FileInputStream("ABC.txt"));

while (dis.available() != 0)
    System.out.println(dis.readDouble());

dis.close();

If you want to know how the data is retrieved and converted, try this如果您想知道如何检索和转换数据,试试这个

byte[] bBuf = new byte[8];
double d = 0;
long l = 0;

FileInputStream fis = new FileInputStream("ABC.txt");

while(-1 != fis.read(bBuf))
{
    l = (((long) bBuf[0] & 0xff) << 56)
        | (((long) bBuf[1] & 0xff) << 48)
        | (((long) bBuf[2] & 0xff) << 40)
        | (((long) bBuf[3] & 0xff) << 32)
        | (((long) bBuf[4] & 0xff) << 24)
        | (((long) bBuf[5] & 0xff) << 16)
        | (((long) bBuf[6] & 0xff) << 8)
        | ((long) bBuf[7] & 0xff)
        ;
    d = Double.longBitsToDouble(l);
    System.out.println(d);
}

fis.close();

This isn't a thorough answer, but hopefully it should get you on the right path.这不是一个彻底的答案,但希望它能让你走上正确的道路。

I would recommend some time learning about character encodings and character sets.我建议花一些时间学习字符编码和字符集。 While it is true that every character in US-ASCII is encoded into a single byte (so when you read a single byte you can convert it to a single character), this is not always the case.虽然确实 US-ASCII 中的每个字符都被编码为一个字节(因此当您读取一个字节时,您可以将其转换为单个字符),但情况并非总是如此。 (so a straight cast from one to the other is a bad idea... one method of conversion would be new String(byte[],Charset) , and String#getBytes(String) for the opposite direction. Classes that work with reading/writing character data to/from streams often permit the specification of which character encoding (Charset) to use. (所以从一个直接转换到另一个是一个坏主意......一种转换方法是new String(byte[],Charset)和相反方向的 String#getBytes(String) 。与阅读一起工作的类/向/从流中写入字符数据通常允许指定使用哪种字符编码(Charset)。

By default, if you do not specify an character encoding (which you really always should), Java will attempt to use the platform encoding of your system to read in and write out streams of data.默认情况下,如果您没有指定字符编码(您确实应该这样做),Java 将尝试使用您系统的平台编码来读入和写出数据流。 Internally however, Java holds these characters as Unicode characters (specifically UTF-16)然而,在内部,Java 将这些字符保存为 Unicode 字符(特别是 UTF-16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM