Java FileOutputStream简介
Java FileOutputStream是用于将数据写入File的输出流,FileOutputStream是OutputStream的子类。
FileOutputStream类用于输出原始字节流,比如:图片、视频等。
Java FileOutputStream构造函数
1.创建文件输出流以写入具有指定名称的文件
public FileOutputStream(String name) throws FileNotFoundException {}
2.创建文件输出流,写入由指定的File对象表示的文件
public FileOutputStream(String name, boolean append) throws FileNotFoundException{}
3.创建文件输出流以写入由指定的File对象表示的文件
public FileOutputStream(File file, boolean append) throws FileNotFoundException {}
4.创建要写入指定文件描述符的文件输出流
public FileOutputStream(FileDescriptor fdObj) {}
Java FileOutputStream核心方法
1、void write(byte[] b)
将 b.length个字节从指定的字节数组写入此文件输出流。
2、oidwrite(byte[] b, int off, int len)
将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
3、voidwrite(int b)
将指定的字节写入此文件输出流。
Java FileOutputStream使用
public class DemoOutputStream { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("D:/fileOutputStream.txt", true); fos.write("Hello World".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
执行结果如下: