简体   繁体   English

编码DataInputStream

[英]Encoding for DataInputStream

I need to encode my files in "ISO-8859-1". 我需要在“ ISO-8859-1”中对文件进行编码。 I know how to do this with a Reader like this: 我知道如何使用这样的Reader来做到这一点:

BufferedReader br = new BufferedReader(new InputStreamReader(
            new FileInputStream(src), "ISO-8859-1"))

But I'm asking how to encode a DataInputStream like this. 但是我在问如何对这样的DataInputStream进行编码。

My decleration right now: 我的天赋:

DataInputStream dit = new DataInputStream(new BufferedInputStream(
            new FileInputStream(src)))

I would prefer a solution, where the encoding-parameter is in the decleration. 我希望有一个解决方案,其中编码参数位于去极化中。 The data I want to read has been written with a DataOutputStream. 我要读取的数据已使用DataOutputStream写入。

Import-method and export-method for DataStreams: DataStreams的导入方法和导出方法:

public void importDST(String src) throws FileNotFoundException, IOException{
    try (DataInputStream dit = new DataInputStream(new BufferedInputStream(new FileInputStream(src)))) {
        while(dit.available() > 0) {
            pupils.add(new Pupil(dit.readInt(), dit.readInt(), dit.readUTF(), dit.readUTF(), dit.readChar(),
                    dit.readUTF(), dit.readInt(), dit.readInt(), dit.readInt(), dit.readUTF(), dit.readUTF(), dit.readUTF(), dit.readUTF(),
                    dit.readUTF(), dit.readUTF()));
        }
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }
}

public void exportDST(String dest, ArrayList<Pupil> pupils) throws FileNotFoundException, IOException{
    this.pupils = pupils;
    try (DataOutputStream dot = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)))) {
        for (Pupil p : this.pupils) {
            dot.writeInt(p.getId());
            dot.writeInt(p.getNumber());
            dot.writeUTF(p.getFirstname());
            dot.writeUTF(p.getLastname());
            dot.writeChar(p.getGender());
            dot.writeUTF(p.getReligion());
            dot.writeInt(p.getDay());
            dot.writeInt(p.getMonth());
            dot.writeInt(p.getYear());
            dot.writeUTF(p.getStreet());
            dot.writeUTF(p.getPlz());
            dot.writeUTF(p.getLocation());
            dot.writeUTF(p.getShortName());
            dot.writeUTF(p.getClassName());
            dot.writeUTF(p.getKvLastname());
        }
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }
}

class Pupil: 班级学生:

public class Pupil implements Serializable{
private int id;
private int number;
private String firstname;
private String lastname;
private char gender;
private String religion;
private int day;
private int month;
private int year;
private String street;
private String plz;
private String location;
private String shortName;
private String className;
private String kvLastname;

public Pupil() {}

public Pupil(int id, int number, String firstname, String lastname, char gender,
             String religion, int day, int month, int year, String street, String plz, String location,
             String shortName, String className, String kvLastname) {
    this.id = id;
    this.number = number;
    this.firstname = firstname;
    this.lastname = lastname;
    this.gender = gender;
    this.religion = religion;
    this.day = day;
    this.month = month;
    this.year = year;
    this.street = street;
    this.plz = plz;
    this.location = location;
    this.shortName = shortName;
    this.className = className;
    this.kvLastname = kvLastname;
}
}

As for ObjectInputStream, the documentation states that 至于ObjectInputStream,文档指出

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. ObjectInputStream反序列化原始数据和先前使用ObjectOutputStream写入的对象。

Also, note: 另外,请注意:

Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams. 只能从流中读取支持java.io.Serializable或java.io.Externalizable接口的对象。

That is, the read data has previously been serialized (or externalized) using ObjectOutputStream and given objects that implement Serializable (or Externalizable). 也就是说,读取的数据以前已使用ObjectOutputStream进行了序列化(或外部化),并且已实现了实现Serializable(或Externalizable)的给定对象。 You would thus deal with charset encoding for any String attributes in the readObject and `writeObject' methods of your Serializable objects. 因此,您将在Serializable对象的readObject和`writeObject'方法中处理所有String属性的字符集编码。

As for DataInputStream, see this answer: DataInputStream and UTF-8 至于DataInputStream,请参见以下答案: DataInputStream和UTF-8

You would have to specify the encoding when creating a String from the read bytes. 从读取的字节创建字符串时,必须指定编码。

Your question doesn't really make sense. 您的问题真的没有道理。

Streams model streams of bytes . 流模型字节流。 They don't have a character encoding, they are just bytes. 它们没有字符编码,只是字节。

Readers read streams of characters . 读者阅读字符流。 These are ultimately streams of bytes too, but there is a character encoding which says how to convert those bytes to chars. 这些最终也都是字节流,但是有一个字符编码,说明如何将这些字节转换为char。 As such, it makes sense to be able to specify this encoding in the constructor. 因此,能够在构造函数中指定此编码是有意义的。

DataInputStream s are Stream s: they read a binary so they don't have a character encoding. DataInputStreamStream :它们读取二进制,因此没有字符编码。

I bypassed this problem by writing and reading just Ints and Bytes, instead of Strings. 我通过仅写入和读取Ints和Bytes而不是String来绕过此问题。 I read bytearrays and made a new String with the encoding out of it. 我读了字节数组,并用其编码制作了一个新的字符串。 Here is the changed code: 这是更改后的代码:

Reading: 读:

public void importDST(String src) throws IOException{
    try (DataInputStream dit = new DataInputStream(new BufferedInputStream(new FileInputStream(src)))) {
        while (dit.available() > 0) {
            Pupil p = new Pupil();
            byte[] arr;
            int len;

            p.setId(dit.readInt());
            p.setNumber(dit.readInt());
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setFirstname(new String(arr, "ISO-8859-1"));
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setLastname(new String(arr, "ISO-8859-1"));
            p.setGender(dit.readChar());
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setReligion(new String(arr, "ISO-8859-1"));
            p.setDay(dit.readInt());
            p.setMonth(dit.readInt());
            p.setYear(dit.readInt());
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setStreet(new String(arr, "ISO-8859-1"));
            p.setPlz(dit.readInt());
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setLocation(new String(arr, "ISO-8859-1"));
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setShortName(new String(arr, "ISO-8859-1"));
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setClassName(new String(arr, "ISO-8859-1"));
            len = dit.readInt();
            arr = new byte[len];
            dit.readFully(arr);
            p.setKvLastname(new String(arr, "ISO-8859-1"));

            pupils.add(p);
        }
    }
}

Writing: 写作:

public void exportDST(String dest, ArrayList<Pupil> pupils) throws IOException{
    this.pupils = pupils;
    try (DataOutputStream dot = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)))) {
        for (Pupil p : pupils) {
            dot.writeInt(p.getId());
            dot.writeInt(p.getNumber());
            dot.writeInt(p.getFirstname().length());
            dot.writeBytes(p.getFirstname());
            dot.writeInt(p.getLastname().length());
            dot.writeBytes(p.getLastname());
            dot.writeChar(p.getGender());
            dot.writeInt(p.getReligion().length());
            dot.writeBytes(p.getReligion());
            dot.writeInt(p.getDay());
            dot.writeInt(p.getMonth());
            dot.writeInt(p.getYear());
            dot.writeInt(p.getStreet().length());
            dot.writeBytes(p.getStreet());
            dot.writeInt(p.getPlz());
            dot.writeInt(p.getLocation().length());
            dot.writeBytes(p.getLocation());
            dot.writeInt(p.getShortName().length());
            dot.writeBytes(p.getShortName());
            dot.writeInt(p.getClassName().length());
            dot.writeBytes(p.getClassName());
            dot.writeInt(p.getKvLastname().length());
            dot.writeBytes(p.getKvLastname());
        }
    }
}

Thanks for all your responses! 感谢您的所有回复!

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

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