利用字节流Outputstream---向E盘的test.txt文件中写入字符串"Hello World!!!"

import java.io.*;        //导入程序相关需要用到的所有IO包

public class file1{

    public static void main(String[] args) throws Exception{                //throws Exception 抛出异常不处理

        File f=new File("e:"+File.separator+"test.txt");                          //File类的定义及实例化

        OutputStream out=new FileOutputStream(f);                            //定义一个输出流对象并实例化

        String s="Hello World!!!";               //定义一个字符串初始化内容

        byte a[]=s.getBytes();                    //只能输出byte数组,所以字符串变为byte数组

        out.write(a);                                   //调用write方法,输出内容

        out.close();                                   //关闭输出流

    }

}

注意:此方法需要事先创建好自己所要写入的空文件 test.txtl;

原文地址:https://www.cnblogs.com/l666/p/9124871.html