IO流1

作者: hey_leex | 来源:发表于2018-01-05 17:07 被阅读0次

IO流一

image

字节流

FileInputStream

FileInputStream(File file) 
      通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。 
FileInputStream(String name) 
      通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。 

int read() 
      从此输入流中读取一个数据字节。 
int read(byte[] b) 
      从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
int read(byte[] b, int off, int len) 
      从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
        off - 目标数组 b 中的起始偏移量。
        len - 读取的最大字节数。 

//逐个字节读入
public static void main(String[] args) throws IOException {
    
    InputStream in = new FileInputStream("a.txt");
    int i = 0;
    while((i = in.read()) != -1) {
        System.out.println((char)i);
    }
    in.close();
}
//带有字节缓冲流的输入流
public static void main(String[] args) throws IOException {
    
    InputStream in = new FileInputStream("a.txt");
    byte[] b = new byte[1024 * 8];
    int len = 0;
    while((len = in.read(b)) != -1) {
        System.out.println(new String(b,0,len));
    }
    in.close();
    
}

FileOutputStream

当指向的文件不逊在世,会自动创建一个,但是创建不了多级目录。

FileOutputStream(File file) 
      创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 
FileOutputStream(File file, boolean append) 
      创建一个向指定 File 对象表示的文件中写入数据的文件输出流。boolean表示是否追加 
FileOutputStream(String name) 
      创建一个向具有指定名称的文件中写入数据的输出文件流。 
FileOutputStream(String name, boolean append) 
      创建一个向具有指定 name 的文件中写入数据的输出文件流。boolean表示是否追加  

void write(byte[] b) 
      将 b.length 个字节从指定 byte 数组写入此文件输出流中。 
void write(byte[] b, int off, int len) 
      将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 
void write(int b) 
      将指定字节写入此文件输出流。 

private static void writeTxtFile(String path) throws IOException {
    // 1:打开文件输出流,流的目的地是指定的文件
    FileOutputStream fos = new FileOutputStream(path);
    // 2:通过流向文件写数据
    fos.write('j');
    fos.write('a');
    fos.write('v');
    fos.write('a');
    // 3:用完流后关闭流
    fos.close();
}

注意:使用write(int b)方法,虽然接收的是int类型参数,但是write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。

private static void writeTxtFile(String path) throws IOException {
    // 1:打开文件输出流,流的目的地是指定的文件
    FileOutputStream fos = new FileOutputStream(path);
    // 2:通过流向文件写数据
    byte[] byt = "java".getBytes();
    fos.write(byt);
    // 3:用完流后关闭流
    fos.close();
}

仔细查看a.txt文本文件发现上述程序每运行一次,老的内容就会被覆盖掉。
那么如何不覆盖已有信息,能够往a.txt里追加信息呢。
查看API文档,发现FileOutputStream类中的构造方法中有一个构造可以实现追加的功能FileOutputStream(File file, boolean append) 
第二个参数,append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处

private static void writeTxtFile(String path) throws IOException {
    // 1:打开文件输出流,流的目的地是指定的文件
    FileOutputStream fos = new FileOutputStream(path,true);

    // 2:通过流向文件写数据
    byte[] byt = "java".getBytes();
    fos.write(byt);
    // 3:用完流后关闭流
    fos.close();
}

文件拷贝

public static void copyFile2(String srcPath, String destPath)
        throws IOException {
    // 打开输入流,输出流
    FileInputStream fis = new FileInputStream(srcPath);
    FileOutputStream fos = new FileOutputStream(destPath,true);

    // 读取和写入信息
    int len = 0;

    // 使用字节数组,当做缓冲区
    byte[] byt = new byte[1024];
    while ((len = fis.read(byt)) != -1) {
        fos.write(byt);
    }

    // 关闭流
    fis.close();
    fos.close();
}

字节流的异常处理

public static void copyFile(String srcPath, String destPath) {

    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(srcPath);
        fos = new FileOutputStream(destPath);

        byte[] byt = new byte[1024 * 1024];
        int len = 0;
        while ((len = fis.read(byt)) != -1) {

            fos.write(byt, 0, len);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);//抛出 catch后的代码不会执行
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

}

在最后的close代码中可能会有问题,两个close,如果第一个close方法出现了异常,并抛出了运行时异常,那么程序还是停止了。下面的close方法就没有执行到。
那么为了保证close的执行,将第二个放到fianlly中即可。

public static void copyFile(String srcPath, String destPath) {

    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(srcPath);
        fos = new FileOutputStream(destPath);

        byte[] byt = new byte[1024 * 1024];
        int len = 0;
        while ((len = fis.read(byt)) != -1) {

            fos.write(byt, 0, len);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {

        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

相关文章

  • java基础-day20-IO流和StringBuffer

    IO流和StringBuffer 1. IO流 1.1 IO流概述 1.2 IO流分类 1.3 文件操作输入输出字...

  • 流(IO)1

    流(IO):(input/output):实现的是两个设备之间的数据传输设备:网络、磁盘(硬盘)、内存、控制台、键...

  • IO流1

    IO流一 字节流 FileInputStream FileOutputStream 当指向的文件不逊在世,会自动创...

  • IO(字符流)&字符流其他内容&递归

    day21(IO(字符流)&字符流其他内容&递归) 1_IO流(字符流FileReader) 1.字符流是什么字符...

  • IO(其他流)&Properties

    day22(IO(其他流)&Properties) 1_IO流(序列流)(了解) 1.什么是序列流序列流可以把多个...

  • JavaSE进阶九 IO流一

    1,IO流的概述 IO流,什么是IO? I:Input O:Output 通过IO可以完成硬盘文件的读和写。...

  • JavaSE知识点15java输入输出流详解

    1 什么是IO? 1 Java的IO机制有流IO和块IO两种,核心库 java.io是大多数面向数据流的IO类的主...

  • 【JavaSE(十三)】JavaIO流(中)

    1 IO流 1.1 IO流概述 Java中使用 IO流 来读取和写入,读写设备上的数据、硬盘文件、内存、键盘等等,...

  • Java IO流(1)

    一、IO流的概念   流是一个抽象的概念。当Java程序需要从数据源读取数据时,会开启一个到数据源的流。数据源可以...

  • #1# IO流 ~ 1

    1.IO流的基础 File implements Serializable,Comparable 1.基本定...

网友评论

      本文标题:IO流1

      本文链接:https://www.haomeiwen.com/subject/maysnxtx.html