zoukankan      html  css  js  c++  java
  • Java基础知识强化之IO流笔记18:FileOutputStream写入数据

    1. 创建字节输出流对象,做了几件事情:

    (1)调用系统功能去创建文件
    (2)创建fos对象
    (3)把fos对象指向这个文件

    2. 代码示例:

     1 package com.himi.fileoutputstream;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 
     8 /*
     9  * 创建文件fos.txt,写入字符串:hello world
    10  * 
    11  * 字节流输出的操作流程:
    12  * A:创建字节输出流对象
    13  * B:写数据
    14  * C:释放资源
    15  */
    16 
    17 public class FileOutputStreamDemo1 {
    18 
    19     public static void main(String[] args) throws IOException {
    20         //FileOutputStream(File file) 
    21 //        File file  = new File("fos.txt");
    22 //        FileOutputStream fos = new FileOutputStream(file);
    23         
    24         //FileOutputStream(String filename) 
    25         /**
    26          * 创建字节输出流对象做了几件事情:
    27          * A:调用系统功能去创建文件
    28          * B:创建fos对象
    29          * C:把fos对象指向这个文件
    30          */
    31         FileOutputStream fos1 = new FileOutputStream("fos.txt");
    32         
    33         //写入数据
    34         String str = new String("hello,world");
    35         byte[] bytes = str.getBytes();
    36         fos1.write(bytes);
    37         //释放资源,让流对象变成垃圾,这样就可以被垃圾回收站回收了
    38         fos1.close();
    39 
    40     }
    41 
    42 }

    输出结果如下:

  • 相关阅读:
    绿豆加速器
    电脑派位系统(新生入学摇号) v2016
    硬盘安装win10
    msbuild
    async
    win sshd
    Ftp软件
    nginx basic auth 登陆验证模块
    深入理解docker的link机制
    Docker Compose to CoreOS
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4850707.html
Copyright © 2011-2022 走看看