Java输入与输出

字节数组输入流:ByteArrayInputStream类

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayTester {
    public static void main(String args[]) throws IOException {
        byte[]buff=new byte[]{2,15,67,-1,-9,9};
        ByteArrayInputStream in = new ByteArrayInputStream(buff,1,4);
        int data = in.read();
        while(data!=-1){
            System.out.print(data+" ");
            data=in.read();
        }
        in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作
    }
}

字节类型15 的二进制形式为:00001111,字节类型15转换为Int类型仍是15。

字节类型-1的二进制形式为11111111,字节类型-1转换为int类型为255

字节类型-9的二进形式为11110111,字节类型-9转换为int类型为247

以上字节数组输入流读取了4个字节后,就到达了输入流的末尾,再执行read()方法,就会返回-1.以上程序的打印结果为;

15 67 255 247

文件输入流:FileInputStream类

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamTester {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("D:\\test.txt");
        int data;
        while ((data=in.read())!=-1)
            System.out.print(data + " ");
        in.close();
    }
}

假设在test.txt文件中包含的字符内容为"abc1好",并且假高文件的所在的操作系统的默认字符编码为GBK,那么在文件中实际上存放的是这5个字符的GBK编码。文件输入流的read()方法每次读取一个字节,因此以上程序的打印结果为:

97 98 99 49 186 195

如果文件很大,为了提高读文件的效率,可以使用使用read(byte[] buff)方法,它能减少物理读文件的次数。

public class UseBuffer {
    public static void main(String[] args) throws IOException {
        final int SIZE=1024;
        FileInputStream in = new FileInputStream("D:\\test.txt");
        FileOutputStream out = new FileOutputStream("D:\\out.txt");

        InputStream in1 = UseBuffer.class.getResourceAsStream("text.txt");//适用于静态方法或实例方法


        byte[] buff=new byte[SIZE]; //创建字节数组缓冲区

        int len=in.read(buff); //把test.txt文件中的数据读入到buff中
        while(len!=-1){
            out.write(buff,0,len);//把buff中的数据写到out.txt文件中
            len=in.read(buff);
        }
        in.close();
        out.close();
    }

    public void copyfile(){
        InputStream in2 = this.getClass().getResourceAsStream("text.txt");//适用于实例方法
    }

管道输入流:PipedInputStream

管道输入流从一个管道输出流中读取数据。通常由一个线程向管道输出流写数据,由另一个线程从管道输入流中读取数据,两个线程可以用管道来通信。当线程A执行管道输入流的read()方法时,如果暂时还没有数据,那么这个线程就会被阻塞,只有当线程B向管道输出流写了新的数据时,线程A才会去恢复运行。

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/*向管道输出流写数据的线程*/
class Sender extends Thread{
    private PipedOutputStream out = new PipedOutputStream();
    public PipedOutputStream getPipedOutputStream(){return out;}

    public void run(){
        try {
            for(int i=-127;i<=128;i++){
                out.write(i);
                yield();
            }
            out.close();
        }catch (Exception e){throw new RuntimeException(e);}
    }

}
/*从管道输入流读数据的线程*/
public class Receiver extends Thread {
    private PipedInputStream in;
    public Receiver(Sender sender) throws IOException {
        in=new PipedInputStream(sender.getPipedOutputStream());
    }
    public void run(){
        try{
            int data;
            while((data=in.read())!=-1)
                System.out.println(data);
        }catch (Exception e){throw new RuntimeException(e);}
    }

    public static void main(String[] args) throws IOException {
        Sender sender = new Sender();
        Receiver receiver = new Receiver(sender);
        sender.start();
        receiver.start();
    }
}

线程Sender向管道输出流写字节,线程Receiver从管道输入流中读取字节。线程Sender输出的字符序列和线程Receiver读入的字节序列相同

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值