java I/O

来源:互联网 发布:ubuntu 远程 中文乱码 编辑:程序博客网 时间:2024/06/05 12:50

1 Node Stream

These streams are all connected to the file or memory directly.


1.2 use FileInputStream/ FileOutputStream to read and write from file

import java.io.*;public class TestFileInputStream {  public static void main(String[] args) {    int b = 0;    FileInputStream in = null;    try {      in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java");    } catch (FileNotFoundException e) {      System.out.println("can't find the file");       System.exit(-1);    }        try {      long num = 0;      while((b=in.read())!=-1){ //in.read() can read a byte a time or read a byte array a time        System.out.print((char)b);         // change each byte to a corresponding char. charactores may not be displayed        /**         *  byte[] c = new byte[20];         *  while((b=in.read(c))!=-1){         *  System.out.print(new String(c));          *  num++;         *  }       * num++; **/   } in.close(); System.out.println(); System.out.println("read "+num+" bytes"); } catch (IOException e1) { System.out.println("error when reading files"); System.exit(-1); } }}

import java.io.*;public class TestFileOutputStream {  public static void main(String[] args) {  int b = 0;  FileInputStream in = null;  FileOutputStream out = null;  try {    in = new FileInputStream("d:/share/java/HelloWorld.java");    out = new FileOutputStream("d:/share/java/io/HW.java");    while((b=in.read())!=-1){      out.write(b);              // write can write one byte a time or a byte array a time            }    in.close();     out.close();  } catch (FileNotFoundException e2) {    System.out.println("can't find the file"); System.exit(-1);  } catch (IOException e1) {    System.out.println("copy file failed"); System.exit(-1);  }  System.out.println("file copy succeed");  }}


2. Processing Stream

processing Streams can be added to the node stream, offering advanced functions

2.1 BufferedReader& BufferedWriter

The consturctor of BufferedInputStream has an InputStream parameter:

FileInputStream fis =      new FileInputStream("d:\\share\\java\\HelloWorld.java");BufferedInputStream bis =      new BufferedInputStream(fis);

Its often used with  FileInputStream to offer the buffered function to the input stream, but it is still byte stream, which means it can only read byte by byte, and can't read chinese charactors which is combined by 2 bytes.


So Actually the other processing stream: BufferedReader/ BufferedWriter is more popular, they are charactor stream and can read info based on charactor. BufferedReader is used with FileReader(Which is a charactor node stream)

BufferedWriter bw = new BufferedWriter( new FileWriter("d:\\share\\java\\dat2.txt"));BufferedReader br = new BufferedReader(         new FileReader("d:\\share\\java\\dat2.txt"));String s = null;for(int i=1;i<=100;i++){     s = String.valueOf(Math.random());     bw.write(s);     bw.newLine();// read one line, BufferedReader's popular function}



2.2 Converting Stream( InputStreamReader& OutputStreamWriter)

As the consturctor says, both of them as the note stream as parameter, they can be added onnote byte stream, to change them to be charactor stream. Shown as the program blow:

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\bak\\char.txt"));osw.write("mircosoftibmsunapplehp");//the reason we do this way is that we can use the osw.write() to write string to the file, not byte by byte//Reading API, we can find this write a string mehtod is inherited from java.io.wirter classSystem.out.println(osw.getEncoding());

The only reason we use it is we can write a string a time to the file.But I think the combination of OutputStreamWriter and the FileOutputStream as the same function as the single Character Node Stream: FileWriter , we can analysis FileOutputStream, OutputStreamWriter and FileWriter 's write() methods.

First, FileOutputStream

          FileOutputStream doesn't have advanced write methods like write a string a time.

So,Second, it can be used with OutputStreamWriter

               OutputStreamWriter is interited from java.io.Write, which has wirte(String str)


Third, The above 2 are not as good as FileWriter


FileWriter is inherited from OutputStreamWriter and has all the write() methods. Besids, it'sNode Character Stream,which can write to

file directly and based on characters.
原创粉丝点击