File文件流的输出

版权声明:嘻嘻嘻嘻,I see you https://blog.csdn.net/qq_38317309/article/details/85807827

类的定义结构字节数出流OUtPutStraem有两个接口outPutStream{abstract}closeable《Interface》 close
Flushable《Interface 》 flushautocloseable可以发现OutPutStream类中实现了Closeable Flushable 两个接口,跑出IOexception接口比问题出现的晚。内部含有close 和flush方法。write 方法时关键public void write(byte[] b) 将给定的字节数组内容全部输出 public void write(byte[] b,int offset int length) 将部分字节输出 public abstruct void write(int b)throws Exception 抽象方法,输出单个字节outputsream 是一个抽象类,如果想要父类实例化,那么就要使用子类实例化。此处所关注的只关注子类的构造方法,可以使用FileOutput Stream 的类中的方法。接收File类 public FileOutputStream(File file,Boolean append )throws FileNotfoudException 追加,append 为true 时,表示追加关于输出的异常抛出 output.write(str.getBytes(),1,1) 最常用的输出语句

实现文件内容的输出

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;public class TestDemo{
 public static void main(String[] args) throws Exception{
  File file=new File("e:"+File.separator+"hello.txt");
  if(!file.getParentFile().exists()) {
   file.getParentFile().mkdirs();
  }
  //outputStream 是一个抽象类, 需要子类进行实例化,一位置只能进行文件处理
  OutputStream output=new FileOutputStream(file) ;
  //进行文件的输出操作
  String str="helloworld";//要求输出的内容
  output.write(3);//需要把str 的内容转换成字节的形式
  output.close();//关闭输出
 }
}

猜你喜欢

转载自blog.csdn.net/qq_38317309/article/details/85807827