文件流FileOutputStream类

xiaoxiao2021-02-28  76

转载第三方(http://blog.csdn.net/wjg4636266/article/details/6855350)

是OutputStream的子类,提供了文件的基本写入能力,成为文件字节输出流

采用BufferOutputStream 类将FileOutputStream作为参数新建一个对象便可以提高文件的读写效率

如果进行写操作的文件不存在,则自动创建该文件。如果文件所在的路径也不存在则报错。

构造方法:

public FileOutputStream(String name):创建一个具有指定名称的文件中写入数据的输出文件流

public FileOutputStream(String name,boolean append):创建一个向具有指定name的文件中写入数据的输出文件流。如果第二个参数为true,则将字节写入文件末尾处,而不是写入文件开始处

public FileOutputStream(File file):创建一个向指定File对象表示的文件中写入数据的文件输出流

public FileOutputStream(File file,boolean appended):创建一个向指定File对象表示的文件中写入数据的文件文件输出流。如果第二个参数为true,则将字节写入文件末尾处,而不是写入文件开始处。

实例 package com.njty.wjg; import Java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class E_FileOutputStream1 { /** * @param args */ public static void main(String[] args) { byte b [] = {49,50,97,98}; try { FileOutputStream fos = new FileOutputStream("e:\\o.txt",true); FileInputStream fis = new FileInputStream("e:\\o.txt"); for(int i = 0; i<b.length ; i++) fos.write(b[i]); int c = fis.read(); while(c != -1){ System.out.println(c); c = fis.read(); } fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-40321.html

最新回复(0)