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));
        }
    }
  • 相关阅读:
    解析链接部分
    按指定格式输出日期时间
    Comet:基于 HTTP 长连接的“服务器推”技术
    dialog组件
    中文字符截断的问题
    css垂直水平居中方案
    类的创建
    修改placeholder属性
    json化表单数据
    瀑布流布局
  • 原文地址:https://www.cnblogs.com/guwenren/p/2975298.html
Copyright © 2011-2022 走看看