IO包中的其他类
来源:互联网 发布:直播源码下载 编辑:程序博客网 时间:2024/05/01 01:38
序列流:
SequenceInputStream将多个读取流合并成一个读取流。
练习:切割文件
Ps:匿名内部类访问的变量必须是final 的。
练习:
package it.practise.com;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;public class SequenceDemo {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubVector<FileInputStream> v = new Vector<FileInputStream>();v.add(new FileInputStream("G:\\demo\\1.txt"));v.add(new FileInputStream("G:\\demo\\2.txt"));v.add(new FileInputStream("G:\\demo\\3.txt"));Enumeration<FileInputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("G:\\demo\\4.txt");BufferedOutputStream bos = new BufferedOutputStream(fos);byte[] buf = new byte[1024];int len = 0;while((len = sis.read(buf)) != -1) {bos.write(buf, 0, len);bos.flush();}bos.close();sis.close();}}
对象的序列化
ObjectOutputStream
构造方法 ObjectOutputStream(OutputStream out)
重点方法:
void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
ObjectInputStream
构造方法 ObjectInputStream(InputStream in)
重点方法:
Object readObject() 从 ObjectInputStream 读取对象。
注意:因为如果制定的文件里存储的是其他数据,readObject() 方法会返回不了对象,因此会引起 ClassNotFoundException .
package it.practise.com;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class ObjectStreamDemo {public static void main(String[] args) {try {new ObjectStreamDemo().readObj();} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}public void writeObj() throws IOException {ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G:\\demo\\obj.txt"));oos.writeObject(new Person("lisi", 36));oos.close();}public void readObj() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("G:\\demo\\obj.txt"));Person p = (Person)ois.readObject();System.out.println(p);ois.close();}}class Person implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age;Person(String name, int age) {this.name = name;this.age = age;}public String toString() {return name + ":" + age;}}
Ps:(1)想要序列化的对象必须实现 Serializable 接口
(2)没有方法的借口称为标记借口(3)实现 Serializable 接口的类应该自己定义一个serialVersionUID。
(4)static 和 transient 修饰的属性,不能被序列化。
管道流
PipedInputStream PipedOutputStream
输入输出可以直接进行连接,通过结合线程使用。
练习:
package it.practise.com;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class PipedStreamDemo {public static void main(String[] args) throws IOException {PipedInputStream in = new PipedInputStream();PipedOutputStream out = new PipedOutputStream();in.connect(out);Read r = new Read(in);Write w = new Write(out);new Thread(r).start();new Thread(w).start();}}class Read implements Runnable {private PipedInputStream in;Read(PipedInputStream in) {this.in = in;}public void run() {try {byte[] buf = new byte[1024];int len = in.read(buf);String s = new String(buf, 0, len);System.out.println(s);in.close();} catch(IOException e) {throw new RuntimeException("管道读取失败");}}}class Write implements Runnable {private PipedOutputStream out;Write(PipedOutputStream out) {this.out = out;}public void run() {try {out.write("aaaaaaaaaa".getBytes());out.close();} catch(IOException e) {throw new RuntimeException("管道写出失败");}}}
0 0
- IO包中的其他类
- IO包中的其他类
- 黑马程序员----IO包中的其他类
- java学习--IO包中的其他类
- IO包中的其他类总结
- Java IO流--IO包中的其他类
- IO流(四)--IO包中的其他类
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- IO包中的其他对象
- 6.3 在Spring中的编码事务
- Gift Exchanging - UVa 10417 概率dp
- angularjs $evalAsync()实现原理
- 函数的声明和定义
- 标准C语言头文件
- IO包中的其他类
- vim配置文件
- 解决WindowsForm窗体假死的状态
- UGNX多个实体(组件)随机改变颜色GRIP代码
- 消息队列 RabbitMQ系列 第四篇:发布/订阅 Publish/Subscribe
- ios xmpp研究探索:接收消息
- android动效开篇
- linux gedit的配置
- POJ1191 棋盘分割