IO流
IO流本质就是对电脑文件进行读和写的
1.1 缓冲
每次读取4kb效率比不使用缓冲高
1.2 IO流的分类
输入流
从磁盘读取到内存叫输入流
输出流
从内存写入到磁盘叫输出流
1.2.1 字节输入流
将磁盘中的文件读取到内存也就是java代码中
java封装了一个类 FileInputStream
为了提高字节输入效率 使用缓冲类给读取写入增加缓冲区增加读取写入效率 BufferedInputStream
File file = new File("c:/aaa/1.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
fis.close();
bis.close();
//先开的后关 后关的先开
package com.day20_w.a_FileInputStream;
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws IOException {
//将磁盘上面的c:/aaa/1.txt文件这个数据读取到内存中
//加上缓冲效果
//1.创建一个File对象 是咱们本地磁盘的一个File对象
File file = new File("c:/aaa/1.txt");
//2.创建文件字节输入流对象 来操作1.txt
//他就是将c:/aaa/1.txt文件转为字节输入流的形式 之后可以按照
FileInputStream fil = new FileInputStream(file);
//3.FileInputStream这个流不具备缓冲效果
//要加上缓冲效果 要是用另为一个流
//BufferdeInputStream 增加缓冲效果
//将FileInputStream传给BufferedInputStream
//此时FileInputStream 就具备缓冲效果
BufferedInputStream bis = new BufferedInputStream(fil);
//4.创建一个缓冲区的数组 这个数组是byte类型的数组
//当缓冲数组为两个字节时 每次读取时存在缓冲数组中 只存2字节
// byte[] buf = new byte[4 * 1024];
byte[] buf = new byte[2];
//现在开始读取数据
//5.开始读取数据 read方法
// int read = bis.read(buf);
// System.out.println(new String(buf , 0 , read));
int length = -1;
while ((length = bis.read(buf)) != -1) {
System.out.print(new String(buf , 0, length));
}
//6.结束流 不结束占内存
fil.close();
bis.close();
}
}
1.2.2 字节输出流
将内存中(java代码)写入到磁盘中
java封装了一个类 FileOutputStream
为了提高字节输入效率 使用缓冲类给读取写入增加缓冲区增加读取写入效率 BufferedOutputStream
File file = new File("c:/aaa/1.txt");
FileOutputStream fis = new FileOutputStream(file);
BufferedOutputStream bis = new BufferedOutputStream(fis);
bos.flush();
//先开的后关 后关的先开
bos.close();
fos.close();
package com.day20_w.b_FileOutputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Demo1 {
public static void main(String[] args) throws IOException {
//将java代码中的一个字符串写入到磁盘的某一个文件中
//1.创建一个File对象 告诉编译器要把数据写到那个文件中
File file = new File("c:/aaa/2.txt");
//2.实例化字节输出流对象
FileOutputStream fos = new FileOutputStream(file);
//3.对FileOutputStream加上缓冲效果
BufferedOutputStream bos = new BufferedOutputStream(fos);
//4.将一个字符串写到磁盘中
String str ="大家再等我一天";
//getBytes() 将String类型转为字符数组
byte[] bytes = str.getBytes();
bos.write(bytes);
//刷新流
bos.flush();
//先开的后关 后关的先开
bos.close();
fos.close();
}
}
package com.day20_w.b_FileOutputStream;
import java.io.*;
public class Demo4 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("c:/aaa/4.txt")));
bos.write("嘻嘻哒".getBytes());
bos.flush();
bos.close();
}
}
1.2.3 字节总结
输入和输出流可以写在一行
输入输出结束后要刷新流和结束流
字节流难点(输入输出结合点)
while ((length = bis.read(buf)) != -1) {
bos.write(buf, 0, length);
}
package com.day20_w.b_FileOutputStream;
import java.io.*;
public class Demo5 {
public static void main(String[] args) throws IOException {
copy();
}
public static void copy() throws IOException {
long l = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("c:/bbb/3.mp4")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("c:/aaa/3.mp4")));
byte[] buf = new byte[4 * 1024];
int length;
while ((length = bis.read(buf)) != -1) {
bos.write(buf, 0, length);
}
bis.close();
bos.flush();
bos.close();
long l1 = System.currentTimeMillis();
//加缓冲流所用时间
System.out.println(l1 - l);
}
}
1.2.3 字符输入流【非重点】
字符输入流专门处理字符文件
音频图片视频一般不用 因为涉及到解码 可能会解码失败
开发一般使用字节流!!!
字符输入流java也封装了一个类
FileReader
new File
new FileReader
new BufferedReader
BufferedReader br = new BufferedReader(new FileReader(new File("c:/aaa/1.txt")));
package com.day20_w.c_FileReader;
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws IOException {
//FileReader专门处理字符文件 音频视频图片不能使用这个流
BufferedReader br = new BufferedReader(new FileReader(new File("c:/aaa/1.txt")));
char[] cbf = new char[4 * 1024];
int length;
while ((length = br.read(cbf)) != -1) {
System.out.println(length);
System.out.println(new String(cbf, 0, length));
}
br.close();
}
}
字符输入流封装的方法
一次读两个
length = br.read(cbf, 0 ,2)
一次读一行
String s = br.readLine();
System.out.println(s);
读多行
String str;
while ((str = br.readLine) != null) {
System.out.println(str);
}
package com.day20_w.c_FileReader;
import java.io.*;
public class Demo3 {
public static void main(String[] args) throws IOException {
//FileReader专门处理字符文件 音频视频图片不能使用这个流
BufferedReader br = new BufferedReader(new FileReader(new File("c:/aaa/1.txt")));
char[] cbf = new char[4 * 1024];
int length;
//一次读两个
//length = br.read(cbf, 0 ,2)
//一次读一行
//String s = br.readLine();
//System.out.println(s);
//读多行
//String str;
//while ((str = br.readLine) != null) {
// System.out.println(str);
// }
while ((length = br.read(cbf)) != -1) {
System.out.println(length);
System.out.println(new String(cbf, 0, length));
}
br.close();
}
}
1.2.4 字符输出流
使用FileWriter
new File
new FileWriter
new BufferedWriter
BufferedWriter bw =new BufferedWriter(new FileWriter(new File("c:/aaa/1.txt")));
package com.day20_w.d_FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
BufferedWriter bw =new BufferedWriter(new FileWriter(new File("c:/aaa/1.txt")));
String str = "abcdef123456";
char[] chars = str.toCharArray();
bw.write(str);
bw.flush();
bw.close();
}
}
字符输出流封装的方法
写完下一行换行 换行
bw.newLine();
package com.day20_w.d_FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
BufferedWriter bw =new BufferedWriter(new FileWriter(new File("c:/aaa/1.txt")));
String str = "abcdef";
char[] chars = str.toCharArray();
bw.write(str, 0, 3);
bw.write("hh");
//写完下一行换行 换行
bw.newLine();
bw.write("xx");
bw.newLine();
bw.write("ll");
bw.flush();
bw.close();
}
}
1.2.5 字符总结
package com.day20_w.e_Case;
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("c:/aaa/斗破苍穹.txt")));
BufferedWriter bw =new BufferedWriter(new FileWriter(new File("c:/bbb/斗破苍穹.txt")));
char[] cbf = new char[4 * 1024];
int length;
while ((length = br.read(cbf)) != -1 ) {
bw.write(cbf, 0, length);
}
bw.flush();
br.close();
bw.close();
}
}