此文档摘自API
An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.
下面的是程序
package io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataOutputStreamtest1 {
public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("f:/java.txt")));
byte b = 3;
int a = 5;
float fl = 1.49f;
boolean bo = false;
dos.write(b);
//dos.write(a);
dos.writeInt(a);
dos.writeFloat(fl);
dos.writeBoolean(bo);
dos.close();
InputStream is = new FileInputStream("f:/java.txt");
int length ;
byte[] buffer = new byte[300];
if(-1!= (length = is.read(buffer,0,300)));
String str = new String(buffer,0,length);
System.out.println(str);
is.close();
//读和写的顺序一定要保持严格一致
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("f:/java.txt")));
System.out.println(dis.readByte());
System.out.println(dis.readInt());
System.out.println(dis.readFloat());
System.out.println(dis.readBoolean());
dis.close();
}
}
写入文档的二进制编码是 ?靖R |