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();
             } }
  • 相关阅读:
    SD卡image 的文件系统分区太小无法安装更多库(如何扩大SD卡rootfs分区)
    ubuntu下生成dtb文件提示:sopc2dts: command not found
    HPS端用于信息打印窗口的 串口的驱动是什么时候加载的呢?
    给 HPS 增添了一个用FPGA逻辑自定义的外设以后, SD卡 image里面哪些文件要更新?
    为何FPGA 外设 IP 与 HPS IP 之间有个 Avalon-MM Pipeline Bridge IP?
    什么是根文件系统(root filesystem)(未完待续)
    为什么preloader和uboot要放置在RAW A2分区?
    重装系统的时候最好选择?(未完待续)
    Android相关知识点面试
    java知识查漏补缺
  • 原文地址:https://www.cnblogs.com/BonnieWss/p/10910931.html
Copyright © 2011-2022 走看看