字节流和字符流的read方法

字节流和字符流的read方法

public class Test {
    public void fileOutput() throws Exception {
        File file = new File("D:/2.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        String s = "abcdefg";
        fileOutputStream.write(s.getBytes());
        fileOutputStream.close();
    }

    /**
     * fileInputStream.read()是一个字节一个字节的读,返回值为根据ascii码表转成的int值
     * 输出结果
     97
     a
     98
     b
     99
     c
     100
     d
     101
     e
     102
     f
     103
     g
     *
     * @throws Exception
     */
    public void fileInput() throws Exception {
        File file = new File("D:/2.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int a;
        while ((a = fileInputStream.read()) != -1) {
            System.out.println(a);
            System.out.println((char)a);
        }
        fileInputStream.close();
    }

    /**
     * 输出结果:
     * 97
     * 98
     * 99
     * 100
     * 101
     * 102
     * 103
     * [B@5a8e6209
     * abcdefg
     *
     * @throws Exception
     */
    public void fileInput2() throws Exception {
        File file = new File("D:/2.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int a;
        int[] b = new int[10];
        byte[] c = new byte[10];
        int len = 0;
        while ((a = fileInputStream.read()) != -1) {
            b[len] = a;
            c[len] = (byte) a;
            len++;
        }
        for (int i = 0; i < len; i++) {
            System.out.println(b[i]);
        }
        System.out.println(c.toString());
        System.out.println(new String(c));
        fileInputStream.close();
    }

    /**
     * 带参数read(byte[] b)方法,读取参数b字节大小
     * 其返回值为int类型,the total number of bytes read into the buffer, or <code>-1</code> if there is no more data because the end of the file has been reached.
     * 输出结果:
     * 7
     * -1
     * abcdefg
     *
     * @throws Exception
     */
    public void fileInput3() throws Exception {
        File file = new File("D:/2.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] b = new byte[10];
        byte[] bb = new byte[5];
        int a = fileInputStream.read(b);
        int c = fileInputStream.read(bb);
        System.out.println(a);
        System.out.println(c);
        System.out.println(new String(b));
        fileInputStream.close();
    }

    /**
     * 输出结果
     * 7
     * abcdefg
     *
     * @throws Exception
     */
    public void fileInput4() throws Exception {
        File file = new File("D:/2.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] b = new byte[10];
        int a = fileInputStream.read(b, 0, new Long(file.length()).intValue());
        System.out.println(a);
        System.out.println(new String(b));
        fileInputStream.close();
    }

    /**
     * 输出结果:
     * 97
     * 98
     * 99
     * 100
     * 101
     * 102
     * 103
     * @throws Exception
     */
    public void fileReader() throws Exception {
        File file = new File("D:/2.txt");
        FileReader fileReader = new FileReader(file);
        int a;
        while ((a=fileReader.read())!=-1){
            System.out.println(a);
        }
        fileReader.close();
    }

    /**
     * 输出结果:
     * abcdefg
     * @throws Exception
     */
    public void fileReader2() throws Exception {
        File file = new File("D:/2.txt");
        FileReader fileReader = new FileReader(file);
        int a;
        int len=0;
        byte[] b=new byte[10];
        while ((a=fileReader.read())!=-1){
            b[len]=(byte) a;
            len++;
        }
        System.out.println(new String(b));
        fileReader.close();
    }

    /**
     * 输出结果:
     * abcdefg
     * @throws Exception
     */
    public void fileReader3() throws Exception {
        File file = new File("D:/2.txt");
        FileReader fileReader = new FileReader(file);
        char[] cbuf=new char[10];
        fileReader.read(cbuf);
        System.out.println(new String(cbuf));
        fileReader.close();
    }


    public static void main(String[] args) throws Exception {
        Test test = new Test();
        test.fileInput();
         } }

 

转载于:https://www.cnblogs.com/BonnieWss/p/10910931.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值