• java笔记之IO2写操作


    IO流的分类

      流向:

        输入流  读出数据

        输出流 写出数据

      数据类型

        字节流

          字节输入流 读取数据 inputstream

          字节输出流 写出数据 outputstream

        字符流

          字符输入流 读取数据 Reader

          字符输出流 写出数据 Write

    注意:通常情况按照数据类型来区分

    *****需求:向文本文件输出一句话hello world 

      分析:这个时候最好是采用字符流 但是字符流是在字节流之后  故采用的是字节输出流(写一句话)‘

      outputsteam是一个抽象类 所以不能实例化,这样就去寻找子类,因为是文件,然后确定为fileoutputstream(每种基类的子类都是以父类名作为后缀名)

    FileOutputStream的构造方法:
     *   FileOutputStream(File file)
     *  FileOutputStream(String name)

    字节输出流操作步骤:
     *   A:创建字节输出流对象
     *   B:写数据
     *   C:释放资源

     1 public class FileOutputStreamDemo {
     2     public static void main(String[] args) throws IOException {
     3         // 创建字节输出流对象
     4         // FileOutputStream(File file)
     5         // File file = new File("fos.txt");
     6         // FileOutputStream fos = new FileOutputStream(file);
     7         // FileOutputStream(String name)
     8         FileOutputStream fos = new FileOutputStream("fos.txt");
     9         /*
    10          * 创建字节输出流对象了做了几件事情:
    11          * A:调用系统功能去创建文件
    12          * B:创建fos对象
    13          * C:把fos对象指向这个文件
    14          */
    15         
    16         //写数据
    17         fos.write("hello,IO".getBytes());
    18         fos.write("java".getBytes());
    19         
    20         //释放资源
    21         //关闭此文件输出流并释放与此流有关的所有系统资源。
    22         fos.close();
    23         /*
    24          * 为什么一定要close()呢?
    25          * A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
    26          * B:通知系统去释放跟该文件相关的资源
    27          */
    28         //java.io.IOException: Stream Closed
    29         //fos.write("java".getBytes());
    30     }
    31 }
    View Code

    字节输出流操作步骤:
     * A:创建字节输出流对象
     * B:调用write()方法
     * C:释放资源
     *
     * public void write(int b):写一个字节
     * public void write(byte[] b):写一个字节数组
     * public void write(byte[] b,int off,int len):写一个字节数组的一部分

     1 public class FileOutputStreamDemo2 {
     2     public static void main(String[] args) throws IOException {
     3         // 创建字节输出流对象
     4         // OutputStream os = new FileOutputStream("fos2.txt"); // 多态
     5         FileOutputStream fos = new FileOutputStream("fos2.txt");
     6 
     7         // 调用write()方法
     8         //fos.write(97); //97 -- 底层二进制数据    -- 通过记事本打开 -- 找97对应的字符值 -- a
     9         // fos.write(57);
    10         // fos.write(55);
    11         
    12         //public void write(byte[] b):写一个字节数组
    13         byte[] bys={97,98,99,100,101};
    14         fos.write(bys);
    15         
    16         //public void write(byte[] b,int off,int len):写一个字节数组的一部分
    17         fos.write(bys,1,3);
    18         
    19         //释放资源
    20         fos.close();
    21     }
    22 }
    View Code

    * 如何实现数据的换行?
     *   为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
     *   如何实现呢?写入换行符号即可呗。
     *   刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
     *   因为不同的系统针对不同的换行符号识别是不一样的?
     *   windows:
     *   linux:
     *   Mac:
     *   而一些常见的个高级记事本,是可以识别任意换行符号的。
     *
     * 如何实现数据的追加写入?
     *   用构造方法带第二个参数是true的情况即可

     1 public class FileOutputStreamDemo3 {
     2     public static void main(String[] args) throws IOException {
     3         // 创建字节输出流对象
     4         // FileOutputStream fos = new FileOutputStream("fos3.txt");
     5         // 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
     6         FileOutputStream fos = new FileOutputStream("fos3.txt", true);
     7 
     8         // 写数据
     9         for (int x = 0; x < 10; x++) {
    10             fos.write(("hello" + x).getBytes());
    11             fos.write("
    ".getBytes());
    12         }
    13 
    14         // 释放资源
    15         fos.close();
    16     }
    17 }
    View Code
  • 相关阅读:
    linux加载和卸载模块
    java 面试题之银行业务系统
    java 面试题之交通灯管理系统
    java 实现类似spring的可配置的AOP框架
    分析JVM动态生成的类
    最长上升子序列(模板)
    项目管理模式
    让thinkphp 支持ftp上传到远程,ftp删除
    hdu 1280 前m大的数 哈希
    互联网+脑科学,中国脑计划的机会
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/6381261.html
Copyright © 2020-2023  润新知