• 黑马程序员JAVA高级视频_IO输入与输出19天7(字节流File读写操作)


    package string.test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*
     * 字符流
     * FileReader
     * FileWriter
     * 
     * BufferedReader
     * BufferedWriter
     * 字节流
     * InputStream OutputStream
     * 
     */
    public class InputStreamDemo {
    
        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            // writer();
            // reader1();
            //reader2();
            reader3();
    
        }
    
        public static void writer() throws IOException {
            FileOutputStream fos = new FileOutputStream("demo.txt");
            fos.write("guwenrenzaichangsha".getBytes());
            fos.close();
        }
    
        /**
         * 单字节读取
         * 
         * @throws IOException
         */
        public static void reader1() throws IOException {
            FileInputStream fis = new FileInputStream("demo.txt");
            int len = 0;
            while ((len = fis.read()) != -1) {
                System.out.println((char) len);
            }
        }
    
        /**
         * 字节数组读取
         * 
         * @throws IOException
         */
        public static void reader2() throws IOException {
            FileInputStream fis = new FileInputStream("demo.txt");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        }
    
        /**
         * available 方式读取(文件较大不建议使用)
         * 
         * @throws IOException
         */
        public static void reader3() throws IOException {
            FileInputStream fis = new FileInputStream("demo.txt");
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            System.out.println(new String(bytes));
        }
    }
  • 相关阅读:
    Promise原理实现(一):前置知识点
    移动端禁用缩放
    多条命令同时执行的包concurrently
    通过面试题学JavaScript知识(1)
    移动设备适配
    css 文本溢出显示省略号
    变量对象的理解
    7.10 日志
    7.9 日志
    JMETER接口测试之自动化环境的配置
  • 原文地址:https://www.cnblogs.com/guwenren/p/2975298.html
Copyright © 2020-2023  润新知