zoukankan      html  css  js  c++  java
  • Java学习笔记----------------常见IO操作

    1.缓冲输入文件

    /*
     * Copyright (c) 2013. Designed By BaiQiang.All Right Reserved.
     */
    
    /**
     * Created with IntelliJ IDEA.
     * User: 白强
     * Date: 13-12-8
     * Time: 下午12:55
     */
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    public class InputStreamTest {
    
        public static String read(String filename) throws Exception {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            String s;
            StringBuffer sb = new StringBuffer();
            while ((s = br.readLine()) != null) {
                sb.append(s + "
    ");
            }
            br.close();
            return sb.toString();
        }
    
        public static void main(String[] args) throws Exception {
            System.out.println(read("src/InputStreamTest.java"));
        }
    }

    2.从内存读取

    /*
     * Copyright (c) 2013. Designed By BaiQiang.All Right Reserved.
     */
    
    import java.io.StringReader;
    
    /**
     * Created with IntelliJ IDEA.
     * User: 白强
     * Date: 13-12-8
     * Time: 下午1:02
     */
    public class MemoryInput {
    
        public static void main(String[] args) throws Exception {
            StringReader in = new StringReader(
                    InputStreamTest.read("src/MemoryInput.java"));
            int c;
            while ((c = in.read()) != -1)
                System.out.println((char) c);
        }
    
    }

    3.文件写入

    /*
     * Copyright (c) 2013. Designed By BaiQiang.All Right Reserved.
     */
    
    /**
     * Created with IntelliJ IDEA.
     * User: 白强
     * Date: 13-12-8
     * Time: 下午1:03
     */
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.io.StringReader;
    
    public class BasicFileOutput {
    
        static String file = "basie.out";
    
        public static void main(String[] args) throws Exception {
            BufferedReader in = new BufferedReader(new StringReader(
                    InputStreamTest.read("src/BasicFileOutput.java")));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                    file)));
            int lineCount = 1;
            String s;
            while ((s = in.readLine()) != null) {
                out.println(lineCount++ + ": " + s);
            }
            out.close();
            System.out.println(InputStreamTest.read(file));
        }
    }

    4.IO实例

    /*
     * Copyright (c) 2013. Designed By BaiQiang.All Right Reserved.
     */
    
    /**
     * Created with IntelliJ IDEA.
     * User: 白强
     * Date: 13-12-8
     * Time: 下午1:12
     */
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class TextFile extends ArrayList<String> {
    
        private static final long serialVersionUID = 1L;
    
        // Read a file as a String
        public static String read(String filename) {
            StringBuilder sb = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new FileReader(new File(
                        filename).getAbsoluteFile()));
                String s;
                try {
                    while ((s = in.readLine()) != null) {
                        sb.append(s);
                        sb.append("
    ");
                    }
                } finally {
                    in.close();
                }
    
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return sb.toString();
        }
    
        // Write a single file in one method call
        public static void write(String fileName, String text) {
            try {
                PrintWriter out = new PrintWriter(
                        new File(fileName).getAbsoluteFile());
                try {
                    out.print(text);
                } finally {
                    out.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        // Read a file,spilt by any regular expression
        public TextFile(String fileName, String splitter) {
            super(Arrays.asList(read(fileName).split(splitter)));
            if (get(0).equals(""))
                remove(0);
        }
    
        // Normally read by lines
        public TextFile(String fileName) {
            this(fileName, "
    ");
        }
    
        public void write(String fileName) {
            try {
                PrintWriter out = new PrintWriter(
                        new File(fileName).getAbsoluteFile());
                try {
                    for (String item : this)
                        out.println(item);
                } finally {
                    out.close();
                }
    
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    
        }
    
        // test,I have generated a file named data.d at the root
        public static void main(String[] args) {
    
            /* read() test */
            System.out.println(read("data.d")); // testing is OK!
    
            /* write() test */
            write("out.d", "helloworld
    egg"); // testing is OK!
    
            /* constractor test */
            TextFile tf = new TextFile("data.d"); // testing is OK!
    
        }
    
    }

    代码转:http://blog.csdn.net/zhangerqing/article/details/8466532

  • 相关阅读:
    如何从零开始创建一个IT信息系统
    Linux常用命令
    vue.js 3.2.20: 用rem实现移动端和pc的兼容
    vue.js3.2.6:路由处理404报错(vue-router@4.0.11)
    vue.js项目在nginx上部署:使spring后端记录真实ip地址
    vue.js 3.0.5:用vue-i18n开发i18n国际化功能(vue-i18n@9.2.0)
    前台项目基础框架之spring boot后端(spring boot v2.5.4)
    前台项目基础框架之vue前端(vue@3.2.6)
    intellij idea 2021.2:为一个spring boot项目改名
    git:修改项目的remote地址(git version 2.30.2)
  • 原文地址:https://www.cnblogs.com/bq12345/p/3463803.html
Copyright © 2011-2022 走看看