Java学习之IO流(其他操作流 【操作基本数据类型、操作字节数组、操作字符数组、操作字符串】)

操作基本数据类型
DataInputStream、DataOutputStream

1 DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
2 dos.writeUTF("你好");
3 dos.close();
4 
5 DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
6 String str = dis.readUTF();
7 dis.close();

操作字节数组(源和目的都是内存)

ByteArrayInputStream、ByteArrayOutputStream

关闭此流对象无效,原因:此流没有调用底层资源,只是操作内存(byte数组),所以关闭此流,此流还能继续使用

 1 public static void main(String[] args) {
 2     ByteArrayInputStream bis=new ByteArrayInputStream("abc".getBytes());
 3     ByteArrayOutputStream bos=new ByteArrayOutputStream();
 4     
 5     int ch=0;
 6     while((ch=bis.read())!=-1) {
 7         bos.write(ch);
 8     }
 9     
10     System.out.println(bos.toString());
11     }

操作字符数组

CharArrayInputStream、CharArrayOutputStream

操作字符串

StringReader、StringWriter

猜你喜欢

转载自www.cnblogs.com/WarBlog/p/12143980.html