IO流

来源:互联网 发布:手机smali转换java 编辑:程序博客网 时间:2024/04/27 22:57

1.java流式输入/输出原理

2.输入/输出流的分类

字节流:一个字节往外读

字符流:一个字符往外读

节点流和处理流:

3.输入/输出流:

4.InputStream

InputStream的基本用法:

上面有错,从offset位置开始

5.OutStream的基本用法:

flush: 一般在close之前flush,意思是把缓冲区里的数据写出去。

6.Reader

7.Writer的用法

8.见程序 :读写

9.缓冲流:

BufferedInputStream :缓冲字节流,可以把FileInputStream读出的字节放在缓存里,不用一个一个字节读。

[code=java]import java.io.*;
public class TestBufferStream1 {
  public static void main(String[] args) {
    try {
      FileInputStream fis =
              new FileInputStream("d:\\share\\java\\HelloWorld.java");
      BufferedInputStream bis =
              new BufferedInputStream(fis);
      int c = 0;
      System.out.println(bis.read());
      System.out.println(bis.read());
      bis.mark(100);
      for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
        System.out.print((char)c+" ");
      }
      System.out.println();
      bis.reset();
      for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
        System.out.print((char)c+" ");
      }
      bis.close();
    } catch (IOException e) {e.printStackTrace();}
  }
}[/code]

 

[code=java]import java.io.*;
public class TestBufferStream2 {
  public static void main(String[] args) {
    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\share\\java\\dat2.txt"));   //FileWriter是一个字符一个字符的写,不爽!!就在外面套了BufferedWriter
      BufferedReader br = new BufferedReader(
             new FileReader("d:\\share\\java\\dat2.txt"));    //FileReader是一个字符一个字符的读,不爽!!就在外面套了BufferedReader ,这样可以一行一行读(readLine)
      String s = null;
      for(int i=1;i<=100;i++){
        s = String.valueOf(Math.random());
        bw.write(s);
        bw.newLine();
      }
      bw.flush();
      while((s=br.readLine())!=null){
        System.out.println(s);
      }
      bw.close();
      br.close();
    } catch (IOException e) { e.printStackTrace();}
  }
}[/code]

 BufferedReader有一个特别好用的方法,readLine(),可以直接读取一行数据,如上。

 10.转换流 :可以把字节流转换成字符流读出来。很有用!

 [code=java]import java.io.*;
public class TestTransForm1 {
  public static void main(String[] args) {
    try {
      OutputStreamWriter osw = new OutputStreamWriter(
           new FileOutputStream("d:\\bak\\char.txt"));

//FileOutputStream只能一个字节一个字节的读,不爽,而且不太好用,所以用OutputStreamWriter包住FileOutputStream,这样就可以直接写中文了。
      osw.write("mircosoftibmsunapplehp");
      System.out.println(osw.getEncoding());
      osw.close();
      osw = new OutputStreamWriter(
              new FileOutputStream("d:\\bak\\char.txt", true),  // true 的意思是在该文件中添加新写进去的数据,不加true,会把以前的替换掉。
              "ISO8859_1"); // latin-1
      osw.write("mircosoftibmsunapplehp");
      System.out.println(osw.getEncoding());
      osw.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}[/code] 

 [code=java]import java.io.*;
public class TestTransForm2 {
  public static void main(String args[]) {
    InputStreamReader isr =
            new InputStreamReader(System.in);     //System.in相对于System.out ,可以接受键盘的输入。InputStreamReader不能读汉字。
    BufferedReader br = new BufferedReader(isr);  BufferedReader可以读一行readLine() ,键盘输入一行,敲回车,立马输入一行。
    String s = null;
    try {
      s = br.readLine();
      while(s!=null){
        if(s.equalsIgnoreCase("exit")) break;
        System.out.println(s.toUpperCase());
        s = br.readLine();
      }
      br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }                          //阻塞式  System.in  等待输入 ,不输入就不能干别的,停在那边。
} [/code]

 11.数据流 DataIO:属于处理流

 [code=java]import java.io.*;
public class TestDataStream {
  public static void main(String[] args) {
    ByteArrayOutputStream baos =
                        new ByteArrayOutputStream();   //ByteArray是内存字节数组  ,自动建了ByteArray的字节数组 ,ByteArrayOutputStream是字节数组的管道 。
    DataOutputStream dos =
                        new DataOutputStream(baos);  //直接可以把dobule(8个字节)的数直接写进ByteArrayOutputStream ,而不用转换成字符串了。
    try {
      dos.writeDouble(Math.random());
      dos.writeBoolean(true);    // boolean占一个字节 ,dos9个字节了。
      ByteArrayInputStream bais =
          new ByteArrayInputStream(baos.toByteArray());  // baos转换为字节数组 给输入流bais
      System.out.println(bais.available());  // available  存在的 ,表示还有多少个数据给我们读  ,所以输出为9.
      DataInputStream dis = new DataInputStream(bais);  // DataInputStream直接可以把dobule(8个字节)的数直接写进dis    

     System.out.println(dis.readDouble());
      System.out.println(dis.readBoolean());  先写的先读出来 ,先进先出 ,是队列 。栈是先进后出。
      dos.close();  dis.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}[/code]

12. Print 流:

import java.io.*;
public class TestPrintStream1 {
  public static void main(String[] args) {
    PrintStream ps = null;
    try {
      FileOutputStream fos =
              new FileOutputStream("d:\\bak\\log.dat");
      ps = new PrintStream(fos);
    } catch (IOException e) {
      e.printStackTrace();
    }
    if(ps != null){
      System.setOut(ps);   //有点意思 ,把System.out.print的out写成文件里面输出了。
    }
    int ln = 0;
    for(char c = 0; c <= 60000; c++){
      System.out.print(c+ " ");
      if(ln++ >=100){ System.out.println(); ln = 0;}
    }
  }
}

//读整个文件 :

import java.io.*;
public class TestPrintStream2 {
  public static void main(String[] args) {
    String filename = args[0];
    if(filename!=null){list(filename,System.out);}
  }
  public static void list(String f,PrintStream fs){
    try {
      BufferedReader br =
                  new BufferedReader(new FileReader(f));
      String s = null;
      while((s=br.readLine())!=null){
        fs.println(s);           
      }
      br.close();
    } catch (IOException e) {
      fs.println("无法读取文件");
    }
  }
}
 

记录log文件 :

import java.util.*;
import java.io.*;
public class TestPrintStream3 {
  public static void main(String[] args) {
    String s = null;
    BufferedReader br = new BufferedReader(
                        new InputStreamReader(System.in));
    try {
      FileWriter fw = new FileWriter
                           ("d:\\bak\\logfile.log", true); //Log4J
      PrintWriter log = new PrintWriter(fw);
      while ((s = br.readLine())!=null) {
        if(s.equalsIgnoreCase("exit")) break;
        System.out.println(s.toUpperCase());
        log.println("-----");
        log.println(s.toUpperCase());
        log.flush();
      }
      log.println("==="+new Date()+"===");
      log.flush();
      log.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 13 . Object 流:直接将Object写入或写出

transient关键字 、 serializable借口 、 externalizable 借口 。

import java.io.*;

public class TestObjectIO {
 public static void main(String args[]) throws Exception {
  T t = new T();
  t.k = 8;
  FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeObject(t);
  oos.flush();
  oos.close();
  
  FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
  ObjectInputStream ois = new ObjectInputStream(fis);
  T tReaded = (T)ois.readObject();
  System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
  
 }
}

class T
 implements Serializable  //   *** 标记性接口 ,标记给编译器看,表示这个类可以序列化 。不然会出问题,不能序列化
{
 int i = 10;
 int j = 9;
 double d = 2.3;

k = 15;

//  transient int k = 15;   transient修饰成员变量,在序列化的时候不考虑 。  所以读出来时是默认值“0”。
}

 externalizable 借口 ,是Serializable 的子类 。

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击