java 文件字节输出流

 

 

Example10_5.java

import java.io.*;
public class Example10_5 {
   public static void main(String args[]) {
      byte [] a = "新年快乐".getBytes();
      byte [] b = "Happy New Year".getBytes();
      File file = new File("a.txt");                         //输出的目的地
      try{  
         OutputStream out=new FileOutputStream(file);      //指向目的地的输出流
         System.out.println(file.getName()+"的大小:"+file.length()+"字节");//a.txt的大小:0字节
         out.write(a);                                    //向目的地写数据
         out.close();
         out=new FileOutputStream(file,true);             //准备向文件尾加内容
         System.out.println(file.getName()+"的大小:"+file.length()+"字节");///a.txt的大小:8字节
         out.write(b,0,b.length); 
         System.out.println(file.getName()+"的大小:"+file.length()+"字节");///a.txt的大小:8字节
         out.close();
      }
      catch(IOException e) {
          System.out.println("Error "+e);
      }
  }
}

 

posted @ 2016-10-23 19:56  我有一壶酒  阅读(379)  评论(0编辑  收藏  举报