文件流FileOutputStream类

转载第三方(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();
  }
 }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值