I need to encode my files in "ISO-8859-1". I know how to do this with a Reader like this:
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(src), "ISO-8859-1"))
But I'm asking how to encode a DataInputStream like this.
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.
Import-method and export-method for 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;
}
}