黑马程序员--基础--第一篇--RandomAccessFile,PipedInputStream和ByteArrayInputStream类

ASP.Net+Android+IOS开发.Net培训期待与您交流!

RandomAccessFile类

该类是IO包中功能非常强悍的类,它具有随机读写功能,可以通过skipBytes(int x)或 seek(int x)方法操作任意位置字符的文件,其内部是类似封装了一个大型的byte数组,通过指针随机访问,还封装了IO字节输入输出流,可以随机读写,它能够应用于文件多线程下载,提高了下载的速度,另外还可以应用于大文件的转移!

该类有以下特点:

1:该类只能操作文件,不像其他流,能够从键盘读入,控制台输出。
2:该类直属Object,但是其中封装了IO字节输入输出流,所以在IO包中,是唯一一个既能读,又能写的类。
3:通过构造函数知,只能操作文件,而且该类有模式,r(只读,如果没有不会创建文件) rw(只读,只写,如果文件不存在,会创建文件,存在,不会覆盖) rws rwd,后两个不常用。
4:该类取数据时,数据最好有规律,没有规律去相应的数据比较麻烦!

 import java.io.*;                                                                                                  public class RandomAccessFileTest{
	public static void main(String[] args)throws IOException{
		readFile();
		//writeFile();
	}
	
	public static void writeFile()throws IOException{
		RandomAccessFile raf = new RandomAccessFile("d:/tempFile/random.txt","rw");
		raf.write("张三".getBytes());
		raf.writeInt(65);
		//按四个字节将 int 写入该文件,先写高字节,相比较writeInt(int x),该方法只能写后八位
		raf.write("李四".getBytes());
		raf.writeInt(66);
		
		raf.write("王五".getBytes());
		raf.writeInt(87);
		
		raf.seek(8*7);//随机写
		raf.write("周七".getBytes());
		raf.writeInt(85);
		
		raf.close();
	}
	
	public static void readFile()throws IOException{
		RandomAccessFile raf = new RandomAccessFile("d:/tempFile/random.txt","rw");
		byte[] arr = new byte[8];
		System.out.println(new String(arr,0,raf.read(arr)));
		
		System.out.println(new String(arr,0,raf.read(arr)));
		
		System.out.println(new String(arr,0,raf.read(arr)));
		
		//raf.seek(8*7);//随机读
		raf.skipBytes(32);//随机读  两种随机定指针位置的方法,没有上一种好!
		System.out.println(new String(arr,0,raf.read(arr)));
	}
}
PipedInputOutputStream类

该类是从输入流写的数据直接可以写到输出流中,中间不需要经过一个数组,并且输入流和输出流通过两个线程控制,该方法输入流read方法是一个阻塞式方法,如果没有抢到资源的话会一直等待,这个时候其它线程可以运行,直到该线程读到数据。

该类具有以下特点:

1需要注意的是重写run方法时其内部不能再抛异常,必须捕获异常。

2IO中涉及到多线程的就只有该类,当用到线程和IO时可以用到此类。

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;
public class PipedInputOutputStreamTest{
	public static void main(String[] args)throws IOException{
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos  = new PipedOutputStream();
		pis.connect(pos);
		
		Read r = new Read(pis);
		Write w = new Write(pos);
		
		new Thread(r).start();
		new Thread(w).start();
	}

}

class Read implements Runnable{
	PipedInputStream pis = null;
	
	public Read(PipedInputStream pis){
		this.pis = pis;
	}
	public void run() {
		try{
			byte[] arr = new byte[1024];
			System.out.println("等待读入数据");
			int a = pis.read(arr);
			System.out.println("已经读到数据");
			System.out.println(new String(arr,0,a));
			pis.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

class Write implements Runnable{
	PipedOutputStream pos = null;
	public Write(PipedOutputStream pos){
		this.pos = pos;
	}
	public void run(){
		try{
			System.out.println("开始写入数据,请等待5秒");
			Thread.sleep(5000);
			pos.write("piped lai le".getBytes());
			pos.close();
		}catch(IOException e){
			e.printStackTrace();
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	
	}
}

ByteArrayInputStream类

该类和CharArrayInputStream(存储中文) StringReader(操作字符串)功能类似,该类精髓就是用流的思想操作数组,属于特有数据专用类。

该类有以下特点:

1该类是内存流,其间不调用底层资源,所以调用close()方法不管用。

2该输入输出流内部封装一个自动增长的数组缓冲区作为源和目的,该类一new出即有数组作为源和目的,该类不会产生任何IOException,因为不和硬盘,键盘打交道。

import java.io.*;
public class ByteArrayInputOutputStreamTest{
	public static void main(String[] args){
		ByteArrayInputStream bis = new ByteArrayInputStream("ABCDEFG".getBytes());//源,可为文件
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();//目的内部封装了一个可变长度的数组作为目的
		int a = -1; 
		while((a=bis.read())!=-1){//用流的思想操作数组,把数据写到目的流数组中
			bos.write(a);
		}
		
		System.out.println(bos.size());//该会自动增长
		System.out.println(bos.toString());
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值