代码如下:
package studyreflect;
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;
public class DataStreamTest {
public static void main(String[] args) {
try {
FileOutputStream fos=new FileOutputStream("hello.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos);
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();
FileInputStream fis=new FileInputStream("hello.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
byte[] buf=new byte[1024];
int len=dis.read(buf);
System.out.println(new String(buf,0,len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}问题,怎样才能正确读取writeChars和writeBytes写入的字符串,急求大神解答!!
|