前言
输入输出流的使用已经非常的广泛,除过采用了不同的类操作,其方法大同小异。下文针对16种重要的流进行分解,模型化。
一、16种重要流是什么?
二、输入输出流模型化
1.输入流操作
1.创建流对象,加载文件;
FileInputStream inputStream = new FileInputStream("D:\\备份\\test.txt");
2.按照一定的字节进行读取,返回的是读取的字节数
int readCount = inputStream.read(byte[]);
3.可以转换成具体的字符串
new String(byte, 0, readCount);
4.在finally中关闭流
代码实例:
public static void fileInputStream() {
//创建文件输入流对象
//D:\备份\test.txt" idea中路径会自动将\编译成\\
//路径也可以写成D:/备份/test.txt
String path ="D:\\备份\\test.txt";
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
/*一次读取多个字节 */
byte[] b = new byte[40];
//读取到的字节数量(不是字节本身),读取不到的时候,返回-1
int readCount = 0;
while ((readCount = inputStream.read(b)) != -1) {
//读多少个字节就转多少个字节为字符串
System.out.print(new String(b, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
//关闭流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.输出流操作
1.创建流对象,加载文件;
FileOutputStream outputStream = outputStream = new FileOutputStream(path);
2.按照一定的字节进行写入
outputStream.write(byte2);
3.在finally中刷新、关闭流
//刷新
outputStream.flush();
//关闭流
outputStream.close();
代码实例:
public static void outputStreamDemo() {
//定义流
FileOutputStream outputStream = null;
try {
//要写入的文件路径
String path ="D:\\备份\\test.txt";
//这种会清空文件中原有的数据,在进行写入操作
// outputStream = new FileOutputStream("D:\\备份\\test.txt");
//true表示在文件已有字符的末尾添加
outputStream = new FileOutputStream(path, true);
byte[] bytes = {'r', 'g', 'i'};
//写入
outputStream.write(bytes);
String str = "我的名字叫张三";
//字符串转字节数组
byte[] byte2 = str.getBytes();
//写入
outputStream.write(byte2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
//刷新
outputStream.flush();
//关闭流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、16中输入输出流,代码通
- FileInputerStream //文件的字节输入流 字节流,从磁盘到内存
public static void fileInputStream2() {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("D:\\备份\\test.txt");
int available = inputStream.available();//剩下多少个字节没有读。这里获取的是文件的总字节数
inputStream.skip(3);//跳过3个字节
byte[] b = new byte[available]; //不适合太大文件,byte[]数组不能太大
//不用进行循环直接读取
int readCount = inputStream.read(b);
System.out.print(new String(b, 0, readCount));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
//关闭流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- FileOutputStream 输出流(从内存到硬盘)
/**
* 字节流-输出流--从内存到硬盘
*/
public static void fileOutputStream() {
FileOutputStream outputStream = null;
try {
//这种会清空文件中原有的数据,在进行写入操作
// outputStream = new FileOutputStream("D:\\备份\\test.txt");
//在文件已有字符的末尾添加
outputStream = new FileOutputStream("D:\\备份\\test.txt",true);
byte[] bytes = {'r','g','i'};
//写入
outputStream.write(bytes);
String str ="我的名字叫张三";
//字符串转字节数组
byte[] byte2= str.getBytes();
//写入
outputStream.write(byte2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
//刷新
outputStream.flush();
//关闭流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
文件字符输入流
字符流读取文件,只限制普通的文本文件
*/
public static void fileReader() {
FileReader fileReader = null;
try {
//创建字符流
fileReader = new FileReader("D:\\备份\\test.txt");
char[] chars = new char[6]; //按照字符去读,一次读取6个字符
int readCount = 0;
while ((readCount = fileReader.read(chars)) != -1) {
System.out.println(new String(chars, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @Description:字符输出流
* @Author: yz
* @Version: 1.0.0
* @Date: 2022/1/19
*/
public static void fileWriter(){
FileWriter fileWriter = null;
try {
fileWriter =new FileWriter("D:\\备份\\test.txt");
fileWriter.write("战胜疫情!");
fileWriter.write("\n");
char[] chars = {'1','2','撒'};
fileWriter.write(chars);
fileWriter.write(chars,1,2);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 带缓冲的字符输入流
*/
public static void buffered() {
BufferedReader buffRed = null;
try {
//FileInputStream输入流
FileInputStream ios = new FileInputStream("D:\\备份\\test3.txt");
//InputStreamReader转换流
InputStreamReader red = new InputStreamReader(ios);
// 带缓冲的字符输入流
buffRed = new BufferedReader(red);
String line = null;
while ((line = buffRed.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (buffRed != null) {
try {
buffRed.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
主要对输入输出流进行代码分析,如果有用,可以点赞收藏。