Java的输入与输出流InputStream/OutputStream

先看InputStream和FileInputStream的结构

操作输入流的步骤:

  1. 创建源
  2. 选择流
  3. 操作
  4. 释放源

代码示例:

import org.testng.annotations.Test;
import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.创建源
        File file = new File("jerry.txt");

        //2.选择流
        InputStream in = null;

        //3.操作
        try {
            in = new FileInputStream(file);
            int temp;
            while (true) {

                if (!((temp = in.read()) != -1)) break;
                //read()返回是字符的编码,我们要读取的字符是英文,所以转义成char
                System.out.print((char) temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
            //4.关闭流
            //注意:close()要卸载finally里面。
            //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

用public int read(byte[] b)方法

这个方法每次会读取指定长度的字符。

长度有字符数组byte[]b决定。

import org.testng.annotations.Test;

import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.创建源
        File file = new File("jerry.txt");

        //2.选择流
        InputStream in = null;

        //3.操作
        try {
            in = new FileInputStream(file);
            int temp;
            byte[] b=new byte[1024];
            int len=-1;//len代表每次读取到的字符的长度,也就是上面定义的2014,读到末尾返回-1
            while ((len=in.read(b))!=-1){
                //需要用String类来把读取到的字符数组解码
                String str=new String(b,0,len);
                System.out.println(len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.关闭流
                //注意:close()要卸载finally里面。
                //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileInputStram/FileoutputStream:

是字节流,通过字节的方式读取/输出所有的文件类型,比如:图像、视频、文字等等。

FileReader/FileWriter:

全字符请考虑FileReader/FileWriter

FileOutputStream

import org.testng.annotations.Test;

import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.创建源
        File file = new File("jerry.txt");

        //2.选择流
        OutputStream out = null;

        //3.操作
        try {
            out = new FileOutputStream(file);
            String msg = "天生丽质难自弃";
         //因为write方法的参数是一个字节数组,所以需要把字符串转换成字节数组
            byte[] b = msg.getBytes();
            out.write(b);//把整个字节数组全部写进文件
            out.write(b,3,6);//从字节数组第三个开始,写6个字节到文件
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.关闭流
                //注意:close()要卸载finally里面。
                //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileOutputStream(File file),这个构造方法创建的输出流,每写一次就会把原有内容覆盖

FileOutputStream(File file, boolean append),这个构造方法创建的输出流,不会覆盖原有内容,而是在原有内容后面追加。

原文地址:https://www.cnblogs.com/majestyking/p/12416051.html