zoukankan      html  css  js  c++  java
  • 黑马程序员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));
        }
    }
  • 相关阅读:
    [转]maven插件的开发
    开发建议
    [转]利用maven的surefire插件实现单元测试与集成测试
    sonar-maven-plugin错误2
    20145218张晓涵_网络欺诈技术防范
    20145218张晓涵_信息搜集与漏洞扫描
    20145218张晓涵_Exp5 MSF基础应用
    20145218张晓涵 PC平台逆向破解_advanced
    20145218张晓涵 恶意代码分析
    20145218 免杀原理与实践
  • 原文地址:https://www.cnblogs.com/guwenren/p/2975298.html
Copyright © 2011-2022 走看看