privatevoidinOutputStream() {
int[] i = {128, 250, 430, 520, 820};
String file = "d:/inOutputStream.txt";
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
for (int j : i) {
// write int to data output stream
dos.writeInt(j);
}
// force data to the underlying file output stream
dos.flush();
// available stream to be readwhile (dis.available() > 0) {
// read four bytes from data input, return intint k = dis.readInt();
System.out.print(k + " "); // 128 250 430 520 820
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}
涉及编码的字符流操作(补代码)
privatevoidinOutputReader() {
String file = "d:/inOutputReader.txt";
try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
"UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
bufferedWriter.append("中文");
bufferedWriter.append("chinese");
bufferedWriter.flush();
// available stream to be readwhile (br.ready()) {
System.out.print((char)br.read()); // 中文chinese
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}