输入输出字节流

读入:------------------------------------------------------InputStream
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class InputStream_ {
     public static void main(String[] args) {
          File src = new File("D:/javac/1.txt");
          FileInputStream is = null ;
          try {
               is = new FileInputStream(src);
               byte[] car = new byte[10];
               int len = 0;
               while(-1!=(len=is.read(car))) {
                    String s = new String(car, 0, len);
                    System.out.println(s);
               }
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               if(null != is) {
                    try {
                         is.close();
                    } catch (IOException e) {
                         e.printStackTrace();
                    }
               }
          }
     }
}
写出:------------------------------------------------------OutputStream
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
 
public class OutputStream_ {
       public static void main(String[] args) {
            File destination = new File( "d:/javac/1.txt" );
            OutputStream os = null ;
             try {
                  os = new FileOutputStream(destination, true );
                  String s = "hehehhehehe\t\n" ;
                   byte [] data = s.getBytes();
                  os.write(data, 0, data. length );
                  os.flush();
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            } finally {
                   if (os!=null ) {
                         try {
                               os.close();
                        } catch (IOException e) {
                              e.printStackTrace();
                        }
                  }
            }
      }
}
posted @ 2015-04-03 12:32  王八一瓶子  阅读(170)  评论(0编辑  收藏  举报