zoukankan      html  css  js  c++  java
  • 字节输入流写文本文件【OutputStream、FileOutputStream】

    字节输入流写文本文件

    1.OutputStream基类

            作用:把内存中的数据输出到文件中。

        ※OutputStream的常用方法

    方法名称

    说明

    void write(int c)

    写入一个字节数据

    void write (byte[] b)

    写入数组b的所有字节

    void write (byte[]b , int off , int len)

    将字节数组从off位置开始,长度为len的字节数据输出到输出流中

    void close()

    关闭输出流

    2.字节输入流OutputStream

    常用构造方法:

        ※  FileOutputStream(File file【文件数据源】)

           代码示例:   File file=new File (“C:\test.txt”);

                            FileOutputStream fileObject= new FileOutputStream (file);

        ※  FileOutputStream (String name)

           代码示例:   OutputStream fileObject=new FileOutputStream (“C:\test.txt”);

        ※  FileOutputStream (String name, boolean append)//追加

           代码示例:   OutputStream fileObject=new FileOutputStream (“C:\test. txt”, true);

    ★注意:

           1. 第一、二种构造方法在向文件写数据时将覆盖文件中原有的内容。

           2. 使用构造方法创建实例时,若相应文件不存在,就会自动创建一个空文件。

           3. 若文件存在,但代表一个文件目录,则抛出FileNotFoundException异常。

    3.使用FileOutputStream读取文件步骤:

         1.  Import java.io.*;(IOException, OutputStream,  FileOutputStream)

                2.  FileOutputStream fos=new FileOutputStream (“文件路径+文件名”);

                3.  String word=“ 好好学习 ”;                 

          byte [] words = word .getBytes();                  

          //利用write方法将数据写入文件

          fos. write (words);

         4.  fos .close ();

  • 相关阅读:
    【五校联考1day2】JZOJ2020年8月12日提高组T2 我想大声告诉你
    【五校联考1day2】JZOJ2020年8月12日提高组T1 对你的爱深不见底
    JZOJ2020年8月12日提高组反思
    JZOJ2020年8月11日提高组T4 景点中心
    JZOJ2020年8月11日提高组T3 页
    JZOJ2020年8月11日提高组T2 宝石
    JZOJ2020年8月11日提高组T1 密码
    JZOJ2020年8月11日提高组反思
    JZOJ2020年8月10日提高组T3 玩诈欺的小杉
    nw335 debian sid x86-64 --3 linux内核自带
  • 原文地址:https://www.cnblogs.com/yijiaqi/p/6907497.html
Copyright © 2011-2022 走看看