行业资讯
【南京开发培训】Java开发的输出和输入流(三)
2019-11-04

 在java的语言中所以的所有数据都是使用流读写的。那么什么是流呢?

 


 

      

1、PipedInputStream和PipedOutStream管道流

管道流的主要作用是进行两个线程之间的通信,如图:

 

 管道流分为输出管道流(PipedOutStream)和输入管道流(PipedInputStream)。如果要进行管道输出,必须把输出管道流连接到输入管道流上。输出流管道PipedOutStream通过public synchronized void connect(PipedInputStream snk) throws IOException方法与输入管道流管道建立连接,当然也可以反过来,通过输入管道流的public void connect(PipedOutputStream src) throws IOException方法与输出管道流建立连接,其本质也是调用了输出流管道的connect。线程1通过PipedOutStream类调用PipedInputStream的recieve方法将字节流数据写入PipedInputStream的循环缓冲区(或者环形缓冲区)buffer数组中,线程2通过PipedInputStream的read方法从缓冲区的读取数据。PipedInputStream类中定义了两个私有变量in和out用于表示缓冲区存储字节的索引位置和读取字节的索引位置,如果in<-1,表明缓冲区为空,如果in等于out表明缓冲区已经存满,那么线程1会阻塞,等待线程2从缓冲区中读取数据。

public class Sender implements Runnable {

private PipedOutputStream pos = null;

private byte[] bytes = null;

 

public Sender(String data) {

this.pos = new PipedOutputStream();

this.bytes = data.getBytes();

}

 

public void connect(Reciever reciever){

try {

this.pos.connect(reciever.getPis());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

@Override

public void run() {

try {

pos.write(bytes);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

pos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

}

package ustc.maxiaolun.io;

import java.io.*;

 

public class Reciever implements Runnable {

private PipedInputStream pis = new PipedInputStream();

private byte[] b = new byte[1024];

public Reciever() {

}

 

public PipedInputStream getPis() {

return pis;

}

 

public void setPis(PipedInputStream pis) {

this.pis = pis;

}

 

@Override

public void run() {

try {

int len = this.pis.read(b);

System.out.println(new String(b,0,len));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

}

 

 

package ustc.maxiaolun.io;

import java.io.*;

 

 

public class PipeTest {

 

public static void main(String[] args) {

Sender sender = new Sender("Hello World!");

Reciever reciever = new Reciever();

sender.connect(reciever);

new Thread(sender).start();

new Thread(reciever).start();

}

}

结果输出Hello World!。

2、ByteArrayInputStream 和ByteArrayOutputStream 字节数组流

 ByteArrayInputStream可以将字节数组转化为输入流, ByteArrayOutputStream可以捕获内存缓冲区的数据,转换成字节数组。

package ustc.maxiaolun.io;

import java.io.*;

 

public class ByteArrayInputTest throws IOException{

    

    public static void main(String[] args) {

        

        String mes = "hello,world" ;

        byte[] b = mes.getBytes() ;

        

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( b ) ;

        int result = -1  ;

 

        while( ( result = byteArrayInputStream.read() ) != -1){

            System.out.println( (char) result );

        }

        

        try {

            byteArrayInputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

}

效果如下:

 

 

package ustc.maxiaolun.io;

import java.io.*;

 

public class ByteArrayOutputTest {

 

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

 

        String mes = "你好,world" ;

        byte[] b = mes.getBytes() ;

 

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ;

        try {

            byteArrayOutputStream.write( b );

 

            FileOutputStream fileOutputStream = new FileOutputStream( new File( "f:/123.txt" ) ) ;

 

            byteArrayOutputStream.writeTo( fileOutputStream ) ;

 

            fileOutputStream.flush();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }catch (IOException e) {

            e.printStackTrace();

        }finally{

            try {

                byteArrayOutputStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

 

    }

 

}

 

效果如下:

 

 

3、RandomAccessFile  类用于随机文件的创建和访问

RandomAccessFile 虽然属于java.io下的类,但它不是InputStream或者OutputStream的子类;它也不同于FileInputStream和FileOutputStream。FileInputStream 只能对文件进行读操作,而FileOutputStream 只能对文件进行写操作;但是,RandomAccessFile 与输入流和输出流不同之处就是RandomAccessFile可以访问文件的任意地方同时支持文件的读和写,并且它支持随机访问。RandomAccessFile包含InputStream的三个read方法,也包含OutputStream的三个write方法。同时RandomAccessFile还包含一系列的readXxx和writeXxx方法完成输入输出。

   RandomAccessFile父类:java.lang.Object

   所有接口实现:Closeable, DataInput, DataOutput, AutoCloseable

下面的例子中我们首先打开文本文件,并指定读写权限,然后向文件中写入两行文本,然后将文件指针指向第二行开始处,然后读取第二行的内容。

package ustc.maxiaolun.io;

import java.io.*;

public class RandomAccessTest  {

 

    public static void main(String[] args) {

        testRandomAccessFile("c:\\myFile.txt");

    }

    public static void testRandomAccessFile(String filename) {

        RandomAccessFile randomAccessFile = null;

        try {

            //定义要写入文件的字符串

            String line1 = "First line\n";

            String line2 = "Second line\n";

            //创建RandomAccessFile实例,指定读写权限

            randomAccessFile = new RandomAccessFile(filename, "rw");

            //写入字符串

            randomAccessFile.writeBytes(line1);

            randomAccessFile.writeBytes(line2);

            //将文件指针指向line1结尾处

            randomAccessFile.seek(line1.length());

            //声明一个和line2相同长度的字节数组

            byte[] buffer = new byte[line2.length()];

            //读取line2内容

            randomAccessFile.read(buffer);

            //将读取出来的字符串打印到控制台

            System.out.println(new String(buffer));

        } catch (FileNotFoundException ex) {

            ex.printStackTrace();

        } catch (IOException ex) {

            ex.printStackTrace();

        } finally {

            try {

                if (randomAccessFile != null)

                    randomAccessFile.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

}

文件内容:

 

 

控制台输出:

 

 



 

下面给大家分享一下广西威扬对于软件测试的进阶课程大纲:

 

    2019广西威扬软件测试课程要点及大纲

 

2019广西威扬IT职能培训网报方式

 

如果需要详细了解试听或培训课程费用可留下 姓名+联系方式(手机号或微信号),我们会在第一时间为您解答服务!

 

软件测试零基础班

软件测试周末精品班

java开发班

ISTQB考试班

 

更多资讯尽在官方网站

www.njzhenghou.com

咨询热线
预约试听:186 8212 6618
咨询热线:025-58781701
技术热线:186 8212 6618
联系地址
江苏省深圳市宝安区西乡街道劳动路冠润商务大厦13F整层5楼
南宁中心:鼓楼区湖南路16号5楼
武汉中心:江夏区光谷智慧园16栋
联系我们
咨询热线:186 8212 6618
Copyright © 2023 广西威扬官网-专注项目研发 IT培训 学软件测试 软件开发都在威扬 苏ICP备17057415号