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

来源:互联网 发布:sql注入测试网站 编辑:程序博客网 时间:2024/06/05 20:53

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());}}

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

0 0