Java-IO流-输出流

package cn.bruce.IO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//OutStream类 字节输出流
//从java程序中写出文件,每次操作一个字节,可以写任意文件
//方法都是写入的方法write
//write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。
//write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
//write(int b) 将指定的字节写入此输出流。
//close() 关闭此输出流并释放与此流有关的所有系统资源。
//需要使用子类来调用方法入FileOutStream
//流对象创建步骤:1、创建流子类对象,绑定数据目的,2、调用流对象方法write3、close流
public class IODemo1 {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 如果文件不存在 则创建文件,如果已经存在 会覆盖源文件
        FileOutputStream F = new FileOutputStream("E:\AAAA.TXT");
        F.write(49);
        F.write(48);
        F.write(48);
        byte[] b = { 65, 66, 67, 68 };
        F.write(b);
        F.write("我是中国人".getBytes());// 将字符串转为字节数组进行字节流传输
        F.close();
        File f = new File("E:\A\aa.txt");
        //续写文件,不写就是覆盖
        FileOutputStream fout= new FileOutputStream(f,true);
        fout.write("豪情天下".getBytes());
        //在文件中写入换行符
  可以写在上一行末尾,也可以写在这一行的开头
        fout.write("
请我倾城".getBytes());
        fout.close();
        
        
    }
}

原文地址:https://www.cnblogs.com/BruceKing/p/13539522.html