A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 钟志军zzj 中级黑马   /  2015-7-13 22:37  /  419 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast_01;

import java.io.FileOutputStream;
import java.io.IOException;

/*
* 通过字符流往文件中写数据。
*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:调用写数据的方法
* C:释放资源
*/
public class FileOutputStreamDemo {
        public static void main(String[] args) throws IOException {
                // 创建字节输出流对象
                // FileOutputStream fos = new FileOutputStream("a.txt",true);
                FileOutputStream fos = new FileOutputStream("a.txt");

                // 调用写数据的方法
                // 写入一个字节
                // fos.write(97);
                // fos.write(98);
                // fos.write(99);
                // fos.flush();
                // 写一个字节数组
                // byte[] bys = { 97, 98, 99, 100, 101 };
                byte[] bys = "abcde".getBytes();
                // fos.write(bys);
                // 写一个字节数组的一部分
                fos.write(bys,0,2);
               
                // 释放资源
                fos.close();
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马