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 @   阿风小子  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示