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 + "\n");
        }
        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("\n");
                }
            } 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, "\n");
    }

    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\negg"); // testing is OK!

        /* constractor test */
        TextFile tf = new TextFile("data.d"); // testing is OK!

    }

}

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

posted @ 2013-12-08 14:27  强子~Developer  阅读(183)  评论(0编辑  收藏  举报