IO流原理及流的分类FileInputStream类和FileOutputStream类基本介绍
IO
流原理及流的分类
java IO
流工作原理
I/O
是Input【输入流】
/Output【输出流】
的缩写,I/O
技术是非常实用技术,用于处理数据传输。如读、写文件,网络通讯等。Java
程序中,对于数据的输入/输出操作以流【stream】
的方式进行java.io
包下提供了各种 ”流“ 类和接口,用以获取不同种类的数据,并通过方法输入或输出数据- 输入
input
:读取外部数据【如:磁盘,光盘等存储设备的数据】到程序【内存】中 - 输出
output
:将程序【内存】数据输出到磁盘中,光盘等存储设备中
流的分类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
二进制文件比如是:音频,视频,图片是用字节流进行处理的
文本文件是用字符处理的
InputStream
OutputStream
Reader
Writer
这四个类都是抽象类
InputStream
类继承关系图
OutputStream
类继承关系图
Reader
类继承关系图
Writer
类继承关系图
I/O
流体系图
InputStream
字节输入流
InputStream
抽象类是所有类字节输入流的超类
InputStream
类继承关系图
InputStream
常用的子类
FileInputStream
:文件输入流
BufferedInputStream
:缓冲字节输入流
ObjectInputStream
:对象字节输入流
FileInputStream
文件输入流
Constructor and Description |
---|
FileInputStream(File file) 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File 对象 file 命名。 |
FileInputStream(FileDescriptor fdObj) 创建 FileInputStream 通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。 |
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name 命名。 |
常用方法:
int | read() 从该输入流读取一个字节的数据。 |
---|---|
int | read(byte[] b) 从该输入流读取最多 b.length 个字节的数据为字节数组。 |
int | read(byte[] b, int off, int len) 从该输入流读取最多 len 字节的数据为字节数组 |
void | close() 关闭此文件输入流并释放与流相关联的任何系统资源 |
---|
package IO_.fileInputStream;import org.junit.jupiter.api.Test;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/*** @author: 海康* @version: 1.0*/
public class FileInputStream1 {public static void main(String[] args) {String filePath = "e:\\IDEACODE\\javase\\hello.txt";FileInputStream fis = null;try {fis = new FileInputStream(filePath);while (true) {int read = fis.read();// read 返回 -1 表示读取完毕if (read == -1){break;}System.out.print((char) read);// 将其转成 char 字符}} catch (Exception e) {e.printStackTrace();} finally {// 在使用完成后一定要对流进行关闭try {fis.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void testFileInputStream(){String filePath = "e:\\IDEACODE\\javase\\hello.txt";FileInputStream fis = null;try {fis = new FileInputStream(filePath);byte[] inputData = new byte[1024*8];int len = 0;while ((len=fis.read(inputData))!=-1){System.out.println(new String(inputData,0,len));// 将其转成字符串显示}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 在使用完成后一定要对流进行关闭try {fis.close();} catch (IOException e) {e.printStackTrace();}}}
}
OutputStream
文件输出流
FileOutputStream
文件输入流
FileOutputStream
类构造器
Constructor and Description |
---|
FileOutputStream(File file) 创建文件输出流以写入由指定的 File 对象表示的文件。不会自动追加内容 |
FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File 对象表示的文件。 如果需要追加入内容,需要使用这个构造器 |
FileOutputStream(FileDescriptor fdObj) 创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。 |
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。不会自动追加入内容 |
FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。 |
FileOutputStream
类常用方法
Modifier and Type | Method and Description |
---|---|
void | close() 关闭此文件输出流并释放与此流相关联的任何系统资源。 |
void | write(byte[] b) 将 b.length 个字节从指定的字节数组写入此文件输出流。 |
void | write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off 的指定字节数组写入此文件输出流。 |
void | write(int b) 将指定的字节写入此文件输出流。 |
注意是:如果在写入内容时,该文件不存在会自动创建
案例演示:
要求:请使用FileOutputStream在a.txt文件中,写入“hello world”,如果文件不存在,会创建文件
使用String
类中的getBytes
方法,可以将字符串转成Bytes
数组
package IO_.fileOUtputStream_;import org.junit.jupiter.api.Test;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*** @author: 海康* @version: 1.0*/
public class FileOUtputStream1 {public static void main(String[] args) {}@Testpublic void writeFile(){String writeFilePath = "e:\\IDEACODE\\javase\\a.txt";FileOutputStream fos = null;try {fos = new FileOutputStream(writeFilePath);// char 会自动转成 int 类型,int 也可以自动转成 charfos.write('H');// 只能写入一个字符} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void writeFile2(){String writeFilePath = "e:\\IDEACODE\\javase\\a.txt";FileOutputStream fos = null;try {fos = new FileOutputStream(writeFilePath);// 写入一个字符串String write = "hello world";fos.write(write.getBytes());// 一定性写一个字符串} catch (IOException e) {e.printStackTrace();} finally {// 对流的操作一定要关闭流 释放资源try {fos.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void writeFile3(){String writeFilePath = "e:\\IDEACODE\\javase\\a.txt";FileOutputStream fos = null;try {fos = new FileOutputStream(writeFilePath,true);// 可以在尾部进行追加String writeContext = "hello world";fos.write(writeContext.getBytes(),0,writeContext.length());} catch (IOException e) {e.printStackTrace();} finally {// 在使用流时,一定要释放资源try {fos.close();} catch (IOException e) {e.printStackTrace();}}}
}
练习:要求编程完成图片、音乐的拷贝
注意是:在拷贝大文件时一定要使用循环拷贝,而且是使用write(byte[] ,int ,int )
方法
package IO_.fileOUtputStream_;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*** @author: 海康* @version: 1.0*/
public class FileCopy {public static void main(String[] args) {String filePath = "e:\\rose.jpg";String file = "d:\\rose.jpg";FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(filePath);fos = new FileOutputStream(file);byte[] dataFile = new byte[1024*8];int length = 0;while ((length =fis.read(dataFile))!=-1){fos.write(dataFile,0,length);// 一定要使用这个方法,否则会出现问题}System.out.println("拷贝完成!!!");} catch (IOException e) {e.printStackTrace();}finally {// 在使用完成一定要释放资源if (fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
}