码迷,mamicode.com
首页 > 其他好文 > 详细

输入输出字节流

时间:2015-04-03 13:27:58      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

读入:------------------------------------------------------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();
                        }
                  }
            }
      }
}

输入输出字节流

标签:

原文地址:http://www.cnblogs.com/king-/p/4389710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!