当前位置 博文首页 > 用心编码:java 编程思想--I/O系统(二)

    用心编码:java 编程思想--I/O系统(二)

    作者:[db:作者] 时间:2021-09-07 13:31

    一、输入和输出
    1.基础知识
    (1)任何来自 InputStream ,Reader派生的类都含有 read() 方法。
    任何来自OutputStream,Writer 派生的类都含有 writer()方法。
    (2)创建单一的结果流,需要创建多个对象来修饰结果流。
    (3)Java I/O类库需要多种不同功能的组合。
    (4)几乎每次都要对输入进行缓冲。
    2.对IO系统整体了解
    参考博客:http://blog.csdn.net/yhaolpz/article/details/68945336

    3.典型使用
    (1)缓冲输入文件

    /**
     * 读取文件:
     * 1.采用字符输入
     * 2.可以使用以 String 或者 File 作为文件名的 FileInputStrean
     * 3.为了提高速度,需要进行缓冲
     * 4.采用 readLine() 方法,读取文件内容。
     */
    public class BufferedInputFile {
    
        public static String read(String fileName) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(fileName));
                String str;
                StringBuilder sb = new StringBuilder();
                try {
                    while ((str = in.readLine()) != null) {
                        sb.append(str + "\n");
                    }
                    in.close();
                    return sb.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) throws IOException{
            System.out.println(read("F:\\manage\\javase2\\src\\main\\resources\\app.js"));
        }
    
    }

    (2)缓冲输入文件练习

    /**
     * 打开一个文件,将读取的每行数据存储到 LinkedList 中,然后倒序输出。
     */
    public class Test01 {
    
        public static List<String> read(String fileName) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(fileName));
                List<String> lineList = new LinkedList<String>();
                String str;
                try {
                    while ((str = in.readLine()) != null) {
                        lineList.add(str);
                    }
                    in.close();
                    return lineList;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) throws IOException {
            List<String> linkedList = read("F:\\manage\\javase2\\src\\main\\resources\\app.js");
            /*Iterator iterator = linkedList.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }*/
            int length = linkedList.size();
            for (int i = length - 1; i >= 0; i--) {
                System.out.println(linkedList.get(i));
            }
        }
    
    }
    

    (3)从内存输入
    read()方法返回int类型,需要转化为 char 类型。

    /**
     * 从内存输入
     */
    public class Test02 {
    
        public static void main(String[] args) {
            String str = BufferedInputFile.read("F:\\manage\\javase2\\src\\main\\resources\\app.js");
            if (str != null) {
                StringReader in = new StringReader(str);
                int c;
                try {
                    while ((c = in.read()) != -1) {
                        System.out.print((char) c);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    
    cs
    下一篇:没有了