zoukankan      html  css  js  c++  java
  • 字节流和字符流的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();
             } }
  • 相关阅读:
    2017ICPC沈阳赛现场赛 L-Tree (dfs)
    2019西北工业大学程序设计创新实践基地春季选拔赛 D(卢卡斯定理)
    Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)
    模板
    洛谷
    模板
    洛谷
    洛谷
    模板
    模板
  • 原文地址:https://www.cnblogs.com/BonnieWss/p/10910931.html
Copyright © 2011-2022 走看看