当前位置:   article > 正文

黑马程序员——JAVA基础------IO流(二)----字节流_write2outputstream

write2outputstream

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——

一、IO流概述

这里写图片描述

IO流:输入输出流(Input/Output)

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类

根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流

字节流分为字节输出流(OutputStream)和字节输入流(InputStream)

字节输出流(OutputStream)定义
public abstract class OutputStream extands Object implements Closable,Flushable
  • 1

此类抽象类是表示输出字节流的所有类的超累。输出流接受输出字节并将这些字节发送到某个接收器。
这里写图片描述
这里写图片描述

代码演示:

package com.joe.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
 * 字节输出流格式:
 * OutputStream out = new FileOutputStream()
 * try{}
 * catch{}
 * finally{}
 * 
 * 思路:
 * 1、创建输出流对象
 * 2、创建字节数组接收写入数据
 * 3、将数组中的数据遍历输出
 * 4、关闭流对象
 * 
 * 注意事项:一定要关闭流
 * @author joe
 *
 */

public class OutputStreamDemo {

    public static void main(String[] args) {
        // write1();
        write2();
        System.out.println("success");
    }

    /**
     * 字节输出流的方式一:每次输出一个字节
     */
    public static void write1() {
        OutputStream out = null;
        try {
            // 创建一个文件字节输出流对象
            out = new FileOutputStream("d:\\1.txt");

            String info = "hello,IO";
            byte[] bytes = info.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                // 向文件中输出
                out.write(bytes[i]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭流
                if (out != null)
                    out.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }

    /**
     * 字节输出流的方式二:每次输出指定大小的字节
     */
    public static void write2() {
        OutputStream out = null;
        try {
            // 创建一个文件字节输出流对象(参数true表示追加输出)
            out = new FileOutputStream("d:\\1.txt", true);

            String info = "i love java!";
            byte[] bytes = info.getBytes();
            out.write(bytes);// 输出一个字节数组
            // out.write(bytes,0,5);//输出一个字节数组中的指定范围的字节

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
字节输入流(InputStream)定义
public abstract class InputStream extends Object implements Closeable
  • 1

这里写图片描述
这里写图片描述

代码演示:

package com.joe.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 字节输入流格式: InputStream in = new FileInputStream() 
 *              try{} 
 *              catch{} 
 *              finally{}
 * 
 * 思路: 
 * 1、创建输入流对象 
 * 2、定义一个字节,用来保存实际读取的字节数,-1表示没有数据 
 * 3、创建一个字节数组,用来每次读取数据
 * 4、创建一个StringBuilder缓冲区,用来接收数据 
 * 5、输出读取数据 
 * 6、关闭流对象
 * 
 * 注意事项:一定要关闭流
 * 
 * @author joe
 *
 */

public class InputStreamDemo {

    public static void main(String[] args) {
        read1();
        read2();
        read3();
    }

    /**
     * 字节输入流的读取方式一:每次读取一个字节
     */
    public static void read1() {
        InputStream in = null;
        try {
            // 构造一个字节输入流对象
            in = new FileInputStream("d:\\1.txt");
            int b = -1;// 用于保存实际读取字节数,-1表示没有数据
            while ((b = in.read()) != -1) {
                System.out.print((char) b);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节输入流的读取方式二:一次性读取所有字节
     */
    public static void read2() {

        File f = new File("d:\\1.txt");
        // 构造一个字节输入流对象
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            // 根据文件的大小构造字节数组
            byte[] bytes = new byte[(int) f.length()];
            int len = in.read(bytes);
            System.out.println(new String(bytes));
            System.out.println("len=" + len);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节输入流的读取方式三:每次读取指定大小的字节(常用)
     */
    public static void read3() {
        File f = new File("d:\\1.txt");
        // 构造一个字节输入流对象
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            // 指定每次要读取的字节数组
            // 创建一个长度为10的数组,每次至多只能存进10个字节
            byte[] bytes = new byte[10];
            int len = -1;// 每次读取的实际长度
            StringBuilder sb = new StringBuilder();
            while ((len = in.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, len));
            }
            // 输出
            System.out.println(sb);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/771484
推荐阅读
相关标签
  

闽ICP备14008679号