• java中IO流相关知识点


    (一) 下边使用outputStream字节输出流进行写操作

    package zdbIO;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    public class OutputStreamDemo1 {

        /**
         * @throws IOException
         * @throws IOException
         *
         */
        public static void main(String[] args) throws IOException{
            
            /**
             * 使用IO流的具体步骤:
             *             1.使用file找到要操作的文件
             *             2.(使用字节流或字符流的子类来实例化inputStream、outStream、reader、writer)
             *             3.进行读写操作
             *             4.关闭流,除BufferedReader例外
             */
            
            File file = new File("f:"+File.separator+"zdb1.txt");//使用file找到要操作的文件
            OutputStream out = null;
            out = new FileOutputStream(file,true);//使用OutputStream的子类进行实例化
            String str = "XXX的十年人生规划,一定要有个计划这样你的人生才会有明确的方向 不至于迷失。";//要输出的信息
            byte b[] = str.getBytes();//将str变为byte数组
            out.write(b);//写入数据
            out.close();//关闭流
            
        }
        
    }

    (二)下边使用inputStream字节流进行读操作的第一种方法:


    package zdbIO;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;

    /**
     * 通过inputStream字节流来进行读操作
     *
     */
    public class InputStreamDemo {
        public static void main(String[] args) throws Exception {
            //注意此文件必须存在否则会发生java.io.FileNotFoundException异常
            //File.separator表示分隔符,其中在Windows中表示,在unix表示/,这样可以跨平台
            File file = new File("f:"+File.separator+"zdb1.txt");
            InputStream input = null;
            input = new FileInputStream(file);
            byte b[] = new byte[1024];//开辟一块内存用来存储读取的内容
            int len = input.read(b);//将文件读到字符数组中
            //其中new String(byte[]bytes,int offset,int length),
            //表示将创建一个字符串,从offset为开始,长度为length
            System.out.println(new String(b,0,len));
            input.close();
            
        }

    }


    /**
         * 这种方法中开辟空间的大小受到限制,因此可以根据文件的大小来开辟空间的大小,
         * 即可以使用byte b[] = new byte[(int)file.length()],来创建开辟的空间,
         * 然后可以通过read()方法来一个个读取。
         *
         */

    (二)下边使用inputStream字节流进行读操作的第二种方法

    package zdbIO;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;

    public class InputStreamDemo1 {
        public static void main(String[] args) throws Exception {
            File file = new File("f:"+File.separator+"zdb1.txt");
            InputStream input = null;
            input = new FileInputStream(file);
            byte b[] = new byte[(int)file.length()];
            for(int i=0;i<b.length;i++){
                b[i] = (byte)input.read();
            }
            System.out.println(new String(b));    
            input.close();
        }

    }


    (三)下边使用Reader字节流进行读操作的第一种方法:
    package zdbIO;

    import java.io.File;

    import java.io.FileReader;
    import java.io.Reader;

    public class ReaderDemo {
        public static void main(String[] args) throws Exception {
            File file = new File("f:"+File.separator+"zdb1.txt");
            Reader in = null;
            in = new FileReader(file);
            char c[] = new char[1024];
            int len = in.read(c);
            System.out.println(new String(c,0,len));
            in.close();
        }

    }

    (三)下边使用Reader字节流进行读操作的第二种方法:
    package zdbIO;

    import java.io.File;

    import java.io.FileReader;
    import java.io.Reader;

    public class ReaderDemo {
        public static void main(String[] args) throws Exception {
            File file = new File("f:"+File.separator+"zdb1.txt");
            Reader in = null;
            in = new FileReader(file);
            char c[] = new char[1024];
            int len = in.read(c);
            System.out.println(new String(c,0,len));
            in.close();
        }

    }
    (四)下边使用Writer字节流进行写操作的:
    package zdbIO;

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;

    public class WriterDemo {
        /**
         * 可以通过字符流writer来进行写操作
         * @throws IOException
         *
         */
        
        public static void main(String[] args) throws IOException {
            File file = new File("f:"+File.separator+"zdb1.txt");
            Writer writer = null;
            writer = new FileWriter(file, true);
            String str = "你今天好好学习了吗?你今天进步了吗学会每天一番思";
            writer.write(str);
            writer.close();

        }

    }
    (五) 字符流和字节流的区别:
        1.字符流中写操作Writer中,操作完成后需要关闭流或者刷新,否则写入不成功。
        2.字节流直接与文件进行交互的,不需要使用缓存;而字符流是通过缓存与文件进行交互的;
        3.在传输和硬盘上保存的内容都是字节形式,所以字节形式操作较多;而操作文件的时候使用字符流较多;
















































  • 相关阅读:
    IDEA 修改JavaWeb的访问路径
    坦克大战--Java类型 ---- (1)音乐播放
    codeforces 620C
    算法笔记之KMP算法
    算法笔记数组
    26. Remove Duplicates from Sorted Array
    哈夫曼树的证明
    Complete Binary Search Tree
    Root of AVL Tree
    .net framework环境
  • 原文地址:https://www.cnblogs.com/zdb292034/p/6080417.html
Copyright © 2020-2023  润新知