IO流3

public class Test1 {
  public static void main(String[] args) throws Exception {
  //第二个参数,表示是否向末尾追加,true:追加
  //false:不追加(默认)
  OutputStream os = new FileOutputStream("src/第10章/io/b.txt",true);
  byte[] bs = {97,98,99,100,101};
  // os.write(97);
  os.write(bs);
  os.close();
  }
}

public class TestCopyArray {
  public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("src/第10章/io/12.txt");
    OutputStream os = new FileOutputStream("src/第10章/io/b.txt");
    byte[] bs = new byte[1024];
    while(true){
      int length = is.read(bs);
      //从下标0开始,把length长度写到文件中
      os.write(bs,0,length);
      if(length<1024){
        break;
    }
  }
  is.close();
  os.close();
  }
}

注意:a.boolean:表示是否向文件末尾追加,如果是true表示追加,false表示不追加(也就是覆盖),默认值是false;

   b.创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件。

原文地址:https://www.cnblogs.com/dahaoheshan/p/6824112.html