java中的IO:处理流的使用 博客分类: java基础 javaio流处理流缓冲流对象流
程序员文章站
2024-03-24 20:29:40
...
目录
1.桥梁流
2.缓冲流
3.数据流
4.对象流
5.合并流
1.桥梁流
1)InputStreamReader是输入字节流通向输入字符流的桥梁,每次调用read()方法都会导致从底层输入流读取一个或多个字节.
2)OuputStreamWriter是输出字节流通向输出字符流的桥梁,每次调用write()方法都会导致在给定字符集上调用编码转换器.
3)桥梁流虽然是一种处理流,但可将其包装到缓冲流中,已提高效率.
2.缓冲流
缓冲流中比较常用的是BufferedReader和BufferedWriter,BufferedReader中提供了readLine()方法,实现了读取一行文本;BufferedWriter中提供了newLine()方法,实现了写入一个行分隔符.下面我们对java所有源文件内容进行读取搜索,输出包含1.8新特性的文件:
public class Buffer_Reader_Writer { public static void search(File file, String str) throws IOException { File[] files = file.listFiles(); // 判断是否为文件夹,是则递归查找 if (file.isDirectory() && files.length != 0) { for (File sub : file.listFiles()) { search(sub, str); } } else { // 构造缓存输入流并按行读取文件 BufferedReader br = new BufferedReader(new FileReader(file)); String temp = br.readLine(); while (temp != null) { if (temp.contains(str)) { System.out.println(file.getAbsolutePath()); break; } temp = br.readLine(); } br.close(); } } public static void main(String[] args) throws IOException { search(new File("c:\\Program Files\\java\\src1.8"), "@since 1.8"); } }
3.数据流
数据流允许应用程序以与机器无关方式从底层节点流中读写java基本数据类型和String类型.例:
public class DataStreamDemo { public static void write() throws Exception { DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("c:\\a.txt"))); dos.writeInt(89); dos.writeFloat(3.14f); dos.writeBoolean(false); dos.writeUTF("Hello World"); dos.close(); } public static void read() throws Exception { DataInputStream dis = new DataInputStream(new FileInputStream(new File("c:\\a.txt"))); System.out.println(dis.readInt()); System.out.println(dis.readFloat()); System.out.println(dis.readBoolean()); System.out.println(dis.readUTF()); dis.close(); } public static void main(String[] args) throws Exception { write(); read(); } }
注意:读的顺序需要与写的顺序保持一致.
4.对象流
ObjectOutputStream,通过writeObject()方法将java对象写入OutputStream,这个过程称为序列化,实现序列化的类必须实现Serializable接口;ObjectInputStream,通过readObject()方法将之前的对象进行反序列化.例:
public class ObjectStreamDemo { public static void write() throws Exception { A a1 = new A("a", new B(97)); A a2 = new A("b", new B(98)); B b1 = new B(100); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\obj.txt")); oos.writeObject(a1); oos.writeObject(a2); oos.writeObject(b1); oos.close(); } public static void read() throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:\\obj.txt")); A a3 = (A) ois.readObject(); A a4 = (A) ois.readObject(); B b2 = (B) ois.readObject(); System.out.println(a3); System.out.println(a4); System.out.println(b2); ois.close(); } public static void main(String[] args) throws Exception { write(); read(); } } class A implements Serializable { String name; B b; public A(String name, B b) { this.name = name; this.b = b; } public String toString() { return "name:" + name + "\t" + b.toString(); } } class B implements Serializable { int age; public B(int age) { this.age = age; } public String toString() { return "age:" + new Integer(age).toString(); } }
运行结果:
name:a age:97
name:b age:98
age:100
并且在c盘目录下生成obj.txt文件.
5.合并流
SequenceInputStream,表示两个或多个输入流的逻辑串联,第一个输入流开始读取,直到到达包含的最后一个输入流的文件末尾为止.例:
public class Test { public static void main(String[] args) throws Exception { FileInputStream fis1 = new FileInputStream("c:\\sequence1.txt"); FileInputStream fis2 = new FileInputStream("c:\\sequence2.txt"); // 将fis1和fis2串联成sis SequenceInputStream sis = new SequenceInputStream(fis1, fis2); FileOutputStream fos = new FileOutputStream("c:\\sequence3.txt"); int temp = 0; while ((temp = sis.read()) != -1) { fos.write(temp); } fis2.close(); sis.close(); fos.close(); } }