ByteArrayStream、DataStream、PipedStream、RandomAccessFile使用

ByteArrayStream

package iootherstudy;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    
public class ByteArrayStream {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        /**
         * 从内存缓冲数组读取数据
         */
        byte[] but = "中国".getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(but);
        int len = bais.available();
        byte[] buffer = new byte[len];
        bais.read(buffer);
        String str = new String(buffer);
        System.out.println(str);

        /**
         * 往内存中写数据
         */
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        System.out.println("baos.size()" + baos.size());
        baos.write("中国人国好啊".getBytes());
        System.out.println("baos.size()" + baos.size());
        byte[] buta = baos.toByteArray();
        System.out.println("buta.length" + buta.length);
        System.out.println(new String(buta));

        /**
         * 结合数据格式化使用
         */
        DataOutputStream dos = new DataOutputStream(new ByteArrayOutputStream());
        dos.write(98);
    }

}

DataStream

package iootherstudy;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStreamTest {

    public static void main(String[] args) throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(
                "day16/a.txt"));
        int num = 98;
        String str = "abc中国人";
        //        dos.write(num);
        dos.writeUTF(str);
        dos.writeInt(num);
        dos.close();
        DataInputStream dis = new DataInputStream(new FileInputStream(
                "day16/a.txt"));
        //        int numNow = dis.readInt();
        //        System.out.println(numNow);
        String s = dis.readUTF();
        System.out.println(s);
    }
}

PipedStream使用

package iootherstudy;

import java.io.IOException;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    
    public class PipedStreamTest {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            PipedInputStream pis = new PipedInputStream();
            PipedOutputStream pos = new PipedOutputStream(pis);
            pos.write("欢迎访问本网站~~~~".getBytes());
            int len = pis.available();
            byte[] but = new byte[len];
            pis.read(but);
            System.out.println(new String(but));
            pis.close();
            pos.close();
    
        }

}

RandomAccessFile使用

package iootherstudy;

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

public class RandomAccessFileTest {

    /**
     * 软件30次到期,提示
     * @throws IOException 
     * @throws NumberFormatException 
     */
    public static void main(String[] args) throws NumberFormatException, IOException {
        RandomAccessFile raf = new RandomAccessFile("day16/a.txt", "rw");
       /**
        * 第一种
        */
        /* 
        int count = Integer.parseInt(raf.readLine());
        if (count == 30) {
        System.out.println("过期~~~~");
        return;
    }
    count++;
    System.out.println("第" + count + "次使用");
    raf.seek(0);
    raf.write(String.valueOf(count).getBytes());
    raf.close();
    */
    
    /**
     * 第二种
     */
    // 首先写入格式化数据0
    //        raf.writeInt(0);
    int count = raf.readInt();
    if (count > 5) {
        System.out.println("过期");
        return;
    }
    count++;
    System.out.println("第" + count);
    raf.seek(0);
    raf.writeInt(count);
    raf.close();
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值