code_money_guji's Blog

Happy coding

java IO

code_money_guji posted @ 2011年3月01日 02:32 in javaSE , 1383 阅读

参考资料:

              http://download.oracle.com/javase/tutorial/essential/io/  官方io说明库以及常用的一些IO操作.

              http://blog.csdn.net/redv/archive/2005/03/31/334697.aspx 关于BIO的相关性能分析;

              http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/ io性能调优;

              http://tutorials.jenkov.com/java-io/index.html 详细的io类说明

 

此文讲述的是java io, 并非java.nio. 只是针对io的一些操作的记录. .

java IO :

  1 直接读取的方式:      

FileInputStream fis = new FileInputStream(file);
while(fis.read() != -1){...}

 2 使用bufferedInputStream:

 


      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream bis =new BufferedInputStream(fis);

      int b;
      while ((b = bis.read()) != -1) {
         ...
        }

 

3 使用直接的byte[]数组作为缓冲区的方式:

        FileInputStream fis = new FileInputStream(file);
        byte buf[] = new byte[2048];

        int n;
        while ((n = fis.read(buf)) != -1) {
          for (int i = 0; i < n; i++) {
            ...
          }

 以上的三种情况使用,第三种比较有效率. 但是第二种和第三种差别并不是很大.一般使用第二种比较多. 一般可以在使用第三种方式的时候使用: file.length来确定byte[]的长度.

4 将每一行分别缓冲到ArrayList中,方便读取:

 


    private ArrayList list = new ArrayList();
  
    public LineCache(String fn) throws IOException {
      FileReader fr = new FileReader(fn);
      BufferedReader br = new BufferedReader(fr);
      String ln;
      while ((ln = br.readLine()) != null)
        list.add(ln);
      br.close();
    }
  

 

5 使用Tokenization:

 public class token1 {
    public static void main(String args[]) {
     if (args.length != 1) {
       System.err.println("missing filename");
       System.exit(1);
      }
      try {
        FileReader fr = new FileReader(args[0]);
        BufferedReader br = new BufferedReader(fr);
        StreamTokenizer st = new StreamTokenizer(br);
        st.resetSyntax();
        st.wordChars('a', 'z');
        int tok;
        while ((tok = st.nextToken()) !=
            StreamTokenizer.TT_EOF) {
          if (tok == StreamTokenizer.TT_WORD)
            ;// st.sval has token
        }
        br.close();
      }
      catch (IOException e) {
        System.err.println(e);
      }
    }
  }

  

  6 使用ObjectOutputStream 和ObjectInputStream 做对象序列化;

   public class serial1 {
    public static void main(String args[]) {
      ArrayList al = new ArrayList();
      Random rn = new Random();
      final int N = 100000;
  
      for (int i = 1; i <= N; i++)
        al.add(new Integer(rn.nextInt()));
  
      try {
        FileOutputStream fos = new FileOutputStream("test.ser");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(al);
        oos.close();
      }
      catch (Throwable e) {
        System.err.println(e);
      }
    }
  }


解序列化:
 import java.io.*;
 import java.util.*;
  
  public class serial2 {
    public static void main(String args[]) {
      ArrayList al = null;
  
      try {
        FileInputStream fis = new FileInputStream("test.ser");
        BufferedInputStream bis = new BufferedInputStream(fis);
        ObjectInputStream ois = new ObjectInputStream(bis);
        al = (ArrayList)ois.readObject();
        ois.close();
      }
      catch (Throwable e) {
        System.err.println(e);
      }
    }
  }


 

7 如果是存储和读取一些基本对象,可以使用DataOutputStream和DataInputStream来完成:

        FileOutputStream fos =new FileOutputStream("outdata");
        BufferedOutputStream bos =new BufferedOutputStream(fos);
        DataOutputStream dos =new DataOutputStream(bos);
        Random rn = new Random();
        final int N = 10;
        dos.writeInt(N);
        for (int i = 1; i <= N; i++) {
          int r = rn.nextInt();
          System.out.println(r);
          dos.writeInt(r);
        }
        dos.close();

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter