IO包中的其他类【JAVA基础】

管道流:IO流和多线程相结合

字节管道流:PipedOutputStream 和 PipedInputStream。
字符管道流:PipedWriter 和 PipedReader。

案例

package 黑马IO流_其他类;

import java.io.*;
/*
管道流:IO流和多线程相结合
 */
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("piped lai la".getBytes());
			out.close();
		}
		catch(IOException e)
		{
			throw new RuntimeException("管道流输出失败");
		}		
	}
}

对象的序列化:

1.对象序列化必须实现 Serializable接口
注意:静态成员不能被序列化
private static final long serialVersionUID = 42L;//固定的序列化号
2.将对象存储在硬盘上 ObjectOutputStream
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(“obj.txt”));
oos.writeObject(new Person(“list”,39));
3.读取序列化对象 ObjectInputStream
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(“obj.txt”));
Person p=(Person)ois.readObject();

将学生对象存进硬盘和取出

package 黑马IO流_其他类;

import java.io.*;

public class ObjectStreamDemo {

	public static void main(String[] args)throws Exception
	{
		writeObj();
		readObj();
	}
	public static void readObj() throws Exception
	{
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.txt"));
		Person p=(Person)ois.readObject();
		
		System.out.println(p);
		ois.close();
				
	}
	public static void writeObj() throws IOException
	{//对象序列化存储
		ObjectOutputStream oos=
				new ObjectOutputStream(new FileOutputStream("obj.txt"));
		oos.writeObject(new Person("list",39));
		oos.close();
	}

}
class Person implements Serializable
{
	private static final long serialVersionUID = 42L;//固定的序列化号
	private String name;
	private int age;
	Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public String toString() {
		return name+":"+age;
	}
}

随机访问文件RandomAccessFile类

它具备读写功能,
原理:内部封装了字节输入流和输出流。
内部封装了一个数组,而且通过指针对数组的元素进行操作
通过getFilePointer获取指针位置
通过seek改变指针的位置
构造函数
RandomAccessFile raf=new RandomAccessFile(“ran.txt”,“r”);
该类只能操作文件,还有操作模式:只读r,读写rw
只读模式,不会创建文件
读写模式,文件不存在会自动创建

指定读取案例

package 黑马IO流_其他类;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {

	public static void main(String[] args)  throws IOException
	{
		 writeFile();
		 readFile();

	}
	public static void readFile() throws IOException
	{
		RandomAccessFile raf=new RandomAccessFile("ran.txt","r");
		byte[] buf=new byte[4];
		
		//调整对象中的指针
		raf.seek(8);
		
		//跳过指定的字节数
		//raf.skipBytes(8);
		
		raf.read(buf);		
		String name=new String(buf);
		
		int age=raf.readInt();
		System.out.println(name);
		System.out.println(age);
		
		raf.close();
	}
	public static void writeFile2() throws IOException
	{
		RandomAccessFile raf=new RandomAccessFile("ran.txt","rw");
		raf.seek(8*3);//写在指定位置上
		raf.write("周期".getBytes());
		raf.writeInt(103);
		raf.close();
	}
	public static void writeFile() throws IOException
	{
		RandomAccessFile raf=new RandomAccessFile("ran.txt","rw");
		raf.write("李四".getBytes());
		raf.writeInt(97);
		raf.write("王五".getBytes());
		raf.writeInt(99);
		raf.close();
	}

}

运行结果
运行

操作基本数据类型的对象DateStream

DataIputStream与DataOutputStream
构造函数:
new DataOutputStream(new FileOutputStream(“data.txt”));
new DataInputStream(new FileInputStream(“data.txt”));
方法:
dos.writeInt(234);
dos.readInt();
dos.writeUTF();//写入使用UTF-8 修改版格式编码的字符串
dos.readUTF();

用于操作字节数组的对象 ByteArrayStream

ByteArrayInputStream:在构造的时候,需要接受数据源,而且数据源是一个字节数组。
方法:int read() 从这个输入流读取下一个数据字节。
ByteArrayOutputStream:在构造的时候,不用定义数据目的,因为该对象中已经内部封装了 可变长度的字节数组,这就是数据目的地。
方法:writeTo(OutputStream out) 写这个字节数组输出流的完整内容到指定的输出流
因为这两个流对象都操作的数组,并没有使用系统资源,所以,不用进行close关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值