字节流和字符流的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();
         } }

 

posted @ 2019-05-23 11:33  第二人生Bonnie  阅读(1367)  评论(0编辑  收藏  举报