IO流基本概念:
- IO流用来处理设备之间的数据传输(内存和硬盘之间,相当于输入输出流在他们之间建立了一个管道,管道建立肯定要占用资源,所以我们使用输入输出流之后需要关闭流)
Java对数据的操作是通过流的方式,流的分类按操作类型可以分为两种:
- 字节流:字节流可以操作任何数据类型(文本,图片,音频,视频等),在计算机中任何数据都是以字节的形式存储(一般类以 Stream结尾)
- 字符流:字符流只能操作纯字符数据(一般类以 Reader和Writer结尾)
IO流常用父类:
流的体系结构如下
- closeable<可关闭的>(close())
- flushable<可刷新的>(flush())
inputStream / OutputStream / Reader / Writer
所有的字节输入流都是 inputStream的子类
所有的字节输出流都是 OutputStream的子类
所有的字符输入流都是 Reader的子类
所有的字符输出流都是 Writer的子类
以上4个流都实现了closeable接口
以上2个输出流都实现了flushable接口
FileInputStream 和 FileOuputStream
@Test
public void Test1() throws IOException {
//创建FileInputStream的实例同时打开文件
FileInputStream fis = new FileInputStream("E:\\project\\jichuyufa\\src\\day23\\IODemo\\ioTest");
//读取指定文件内容
byte[] b = new byte[3];
int len = fis.read(b);
while (len != -1){
//System.out.println(new String((b,0,len));
len=fis.read(b);
}
//关闭流
fis.close();
}
@Test
public void test2(){
String str = "askdjfkajdskfj";
FileOutputStream fos = null;
try{
//1. 创建 FileOutputStream 的实例,同时打开指定文件
fos = new FileOutputStream("./hello1.txt");
//2. 将指定内容写到目标地点
fos.write(str.getBytes());
}catch(IOException e){
e.printStackTrace();
}finally{
if(fos != null){
//3. 关闭流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
InputStream 和 OutputStream
public class FileCopy {
public static void main(String[] args) throws IOException {
//demo1();
//demo2();
//方式三:定义小数组读取文件,在将小数组中的数据写入文件(开发中标准的文件拷贝写法)
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream fos=new FileOutputStream("c.txt");
byte[] bytes=new byte[1024*2];
int len;
//这里如果不读取字节数组,则len每一次得到的是对应数据的机器码,则拷贝写入的文件大小则为机器码的字节数总和
while((len=fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
}
private static void demo2() throws FileNotFoundException, IOException {
//方式二:整个文件一起读取在拷贝(available())
//该方式如果读取的是大文件,当将整个大文件读取,则会导致内存溢出
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream fos=new FileOutputStream("c.txt");
int available = fis.available();//获取文件大小
//创建一个与该文件一样大小的字节数组
byte[] b= new byte[available];
//将字节数组读取到内存中
fis.read(b);
//将字节数组的数组写到需要拷贝到的文件中
fos.write(b);
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
//方式一 :一个字节一个字节读取拷贝(该方法也是IO流的拷贝核心代码)
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream fos=new FileOutputStream("c.txt");
int a;
while((a=fis.read())!=-1){
fos.write(a);
}
fis.close();
fos.close();
}
}
使用IO流将一个txt文本中的内容写到多个txt文本中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IODemo {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream(new File("E:/eula.1028.txt"));
OutputStream os1 = new FileOutputStream(new File("G:/demo1.txt"));
OutputStream os2 = new FileOutputStream(new File("G:/demo2.txt"));
byte[] buffer = new byte[1024];
int len = 0;
int L = 0;
while ((len = is.read(buffer)) != -1){
if (L < 1024){
os1.write(buffer, 0, len);
L += len;
} else {
os2.write(buffer, 0, len);
L += len;
}
}
is.close();
os1.close();
os2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}