文件输出流

麦克斯-侯的博客 / 2024-10-06 / 原文

public class OutputStreamTest {
    public static void main(String[] args) {
        OutputStream fos=null;
        try{
            //2.创建文件输出对象
            fos = new FileOutputStream("D:\\doc\\test.txt",true);
            //3.执行写操作
            String str ="I love Java";
            byte[] words=str.getBytes();
            fos.write(words,0, words.length);
            System.out.println("文件已写入成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4.关闭输出流
            try{
                if (fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}