集册 Java实例教程 使用BufferedOutputStream将字节写入文件

使用BufferedOutputStream将字节写入文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

485
使用BufferedOutputStream将字节写入文件
 

  

import java.io.BufferedOutputStream;//NowJava.com - 时  代  Java

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

  

public class Main {

        public static void main(String[] args) {

                String strFile = "C:/Folder/BufferedOutputStreamDemo.txt";

                BufferedOutputStream bos = null;

                try

                {

                        FileOutputStream fos = new FileOutputStream(strFile);

                        bos = new BufferedOutputStream(fos);

                        

                        byte b = 66;

                          

                         System.out.println("BufferedOutputStream: writing file");

                          

                         //this will write a character represented by ascii 66
                         /* from 
                         N o w J a v a . c o m - 时代Java*/

                         //i.e. 'B'

                         bos.write(b);

                          

                        System.out.println("BufferedOutputStream : written file");

                }

                catch(FileNotFoundException fnfe)

                {

                        System.out.println("Specified file not found" + fnfe);

                }

                catch(IOException ioe)

                {

                        System.out.println("Error while writing to file" + ioe);

                }

                finally

                {

                        try

                        {

                                if(bos != null)
展开阅读全文