1.IO流
以后开发中会遇到文件的上传和下载,都需要用到IO流
在读取的时候,会将文件(音频 视频等)变成流的形式一点一点来处理,拆解开出来的
1.IO流的分类
1.从磁盘读取数据到内存 (输入流)Input
这里的内存指的是java代码
也就是从磁盘到内存到数据的传递
2.从内存写入数据到磁盘的某一个文件 (输出流)Output
1.1字节输入流
一共有五步:
1.创建一个File类来读取需要的文档数据File file = new File(“D:/ddd”);
2.创建字节输入流,此时本身不具有缓冲效果,一次只能获取一个FileInputStream fis = new FileInputStream(file);
3.创建字节缓冲流BufferedInputStream bis = new BufferedInputStream(fis);
4.创建字节类型的数组,同时可以确定一次提取的数目byte[] bytes = new byte[3];
5.bis.read(bytes),调用read方法并将数组引入进这个方法中
5.通过while循环以便读取所有的数据,new String(bytes , 0 ,length)
package com.qfedu.a_zht;
import java.io.*;
public class Demo3 {
public static void main(String[] args) throws IOException {
// File file = new File("D:/ddd");
// System.out.println(file.mkdirs());//true
//建立一个File类来获取想要读取的数据
File file = new File("D:/ddd/1.txt");
//创建字节输入流
FileInputStream fis = new FileInputStream(file);
//使其具有缓冲效果
BufferedInputStream bis = new BufferedInputStream(fis);
//使用数组确实一次字节提取的数目
byte[] bytes = new byte[3];
int length;
while ((length = bis.read(bytes)) != -1) {
System.out.println(new String(bytes , 0 ,length));
}
//需要对流进行关闭
fis.close();
bis.close();
}
}
1.2字节输出流
1.2.1 执行步骤
1.创建一个File类,读取需要放置内容储存的数据
2.创建字节输出流
3.创建输出缓冲流
4.创建字节数组用于储存
5.使用循环将所有数据存如指定位置中
6.关闭所有流
package com.qfedu.a_zht;
import java.io.*;
public class Demo6 {
public static void main(String[] args) throws IOException {
//创建一个File类读取文档
File file = new File("D:/ddd/4.txt");
//创建字节输出流,此时并没有缓冲效果
FileOutputStream fos = new FileOutputStream(file);
//创建字节缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
//建立需要输出的变量
String str = "abcdefg";//7个字节
byte[] bytes = str.getBytes();//存入数组中
//写一个缓冲流 ,从零开始偏移,写入2个字节
// bos.write(bytes, 0, 2);
// bos.write(bytes, 2, 2);
// bos.write(bytes, 4, 2);
int length;
while ((length = str.length()) != -1) {
bos.write(bytes, 0, length);
}
//ArrayIndexOutOfBoundsException,需要和字节数相同,不能多也不能少
//bos.write(bytes, 6, 2);
bos.flush();
bos.close();
fos.close();
}
}
1.3字符输入流
与字节输入流的功能相同,都是将磁盘数据传递到内存中
但是只能传递txt文档,音频以及视频均不可以传递(其采用数组是字符数组,传递的数据是受限的)
package com.qfedu.a_zht;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Demo10 {
public static void main(String[] args) throws IOException {
//获取具有缓冲效果的字符输入流
BufferedReader bur = new BufferedReader(new FileReader("D:/bbb/1.txt/"));
//建立字符数组
char[] chars = new char[3];//控制每次传递的数目
int length;
//获取所有数据
while ((length = bur.read(chars)) != -1) {
//将数据展现在控制台
System.out.println(new String(chars, 0, length));
}
//关闭流
bur.close();
}
}
1.4字符输出流
将一个字符串类型的数据写入到磁盘中
package com.qfedu.a_zht;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo11 {
public static void main(String[] args) throws IOException {
BufferedWriter buw = new BufferedWriter(new FileWriter("D:/bbb/3.txt"));
String str = "狗蛋今天真滴很生气";
//将数组转化为字符数组
char[] chars = str.toCharArray();
//直接调用输出方法即可,参数引入转为字符数组的数据
//可以控制传入数组的范围
buw.write(chars);
//buw.write(chars, 0 ,2);
//关闭字符输出流
buw.close();
}
}
1.5综合输入和输出
1.5.1字节的综合案例
package com.qfedu.a_zht;
import java.io.*;
public class Demo {
// 3.找一本小说然后进行复制
public static void main(String[] args) throws IOException {
//创建缓冲字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/bbb/3.txt"));
//创建缓冲字节输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/aaa/3.txt"));
//见名知意,使用字节数组
byte[] bytes = new byte[4 * 1024];
int length;
//从磁盘到内存
//从内存到磁盘
while ((length = bis.read(bytes)) != -1) {//读取所有的数据
bos.write(bytes, 0, length);//输出所有的数据
}
bos.flush();
bos.close();
bis.close();
}
}
1.5.2字符的综合案例
package com.qfedu.a_zht;
import java.io.*;
public class Demo12 {
public static void main(String[] args) throws IOException {
//创建输入缓冲字符流
BufferedReader bur = new BufferedReader(new FileReader("D:/bbb/2.txt"));
//创建输出缓冲字符流
BufferedWriter buw = new BufferedWriter(new FileWriter("D:/aaa/1.txt"));
//字符则需要字符的数据类型,也就是char类型
char[] chars = new char[4 * 1024];
int length;
//从磁盘到内存
//从内存到磁盘
while ((length = bur.read(chars)) != -1) {//读取所有的数据
buw.write(chars, 0, length);//输出所有的数据
}
//均需关闭流
buw.flush();
buw.close();
bur.close();
}
}