java学习笔记|输入输出流(二)

Posted by wzc on 2020-01-15

输入输出流(二)

对象流

1.对象输入流 ObjectInputStream

2.对象输出流 ObjectOutputStream

构造方法
1
2
3
4
5
6
7
8
9
//public ObjectInputStream(InputStream in)
//从文件中读取对象
FileInputStream a= new FileInputStream("tom.txt");
ObjectInputStream in = new ObjectInputStream(a);

//public ObjectOutputStream(OutputStream out)
//将对象写入到文件中
FileOutputStream b = new FileOutputStream("tom.txt");
ObjectOutputStream out = new ObjectOutputStream(b);
对象流的成员方法
1
2
3
4
//类的读方法
public final Object readObject() throws IOException, ClassNotFoundException
//类的写方法
public final void writeObject(Object obj) throws IOException

随机读写流

RandomAccessFile:

​ 用来随机读取文件,其功能更完善;

​ 类直接隶属于Object类;

​ 类创建的流的指向既可以作为源,也可以作为目的地;

构造方法
1
2
3
public RandomAccessFile(String name, String mode) throws FileNotFoundException
public RandomAccessFile(File file, String mode) throws FileNotFoundException
//mode 取值:“r”以只读的方式打开文件;“rw”以读写的方式打开文件
成员方法
1
2
3
4
5
6
7
8
//返回当前的文件指针
public long getFilePointer() throws IOException
//把文件指针置于给出的位置POS
public void seek(long pos) throws IOException
//返回文件长度
public long length() throws IOException
//从当前位置开始跳过n个字节
public int skipBytes(int n) throws IOException
image-20200115180936917 image-20200115181018099