IO流基础(二)
目标
- 掌握字节缓冲流
- 掌握字符流
- 掌握字符缓冲流
一、字节缓冲流
本次学习的过程中遇到了缓冲流这个新的概念,为什么会出现缓冲流呢,它是来解决什么问题的呢?
答:从结果上看,用缓冲流进行IO操作比普通字节流要快一些(文件越大效果越明显);从过程中来说缓冲输出流减少了底层系统的调用,详情可以参考官方文档。
1.1 字节缓冲流的基本使用
public class IO1 {
public static void main(String[] args) throws IOException {
/*
为什么用缓存流呢?
普通的流每次读写都会调用底层的系统,而缓冲流则是将数据一次性写入进去,减少了系统调用
*/
/*
使用缓冲流写数据
*/
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("fos.txt"));
bos.write("hello,world!".getBytes(StandardCharsets.UTF_8));
bos.close();
/*
使用缓冲流读字节
*/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fos.txt"));
int b;
while ((b = bis.read()) != -1){
System.out.print((char) b);
}
bis.close();
/*
使用缓冲流读字节数组
*/
BufferedInputStream fis = new BufferedInputStream(new FileInputStream("fos.txt"));
byte[] bt = new byte[1024];
int len;
while ((len = fis.read(bt)) != -1){
System.out.print(new String(bt, 0, len));
}
fis.close();
}
}
1.2 使用字节缓冲流复制视频
public class IO2 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
copy();
long end = System.currentTimeMillis();
System.out.println(String.format("一共花费:"+ (end - start ) + "毫秒"));
}
private static void copy() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test-副本.mp4"));
byte[] by = new byte[1024];
int len;
while ((len = bis.read(by)) != -1){
bos.write(by, 0, len);
};
bis.close();
bos.close();
}
}
二、字符流
在之前字节流的学习中,我们可以实现文件的读写,当然也可以读写中文汉字等,但是最后无论是读还是写都是通过字节来进行的,每次都要转换会非常麻烦,所以字符流就出来了解决了这个问题,官方解释InputStreamReader是从字节流到字符流的桥。
2.1 字符串的编码和解码
public class IO1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String a = "你好世界";
// 使用utf-8规范编码
byte[] bytes = a.getBytes("UTF-8");
System.out.println(Arrays.toString(bytes));
// 使用utf-8规范解码
System.out.println(new String(bytes, "UTF-8"));
}
}
2.2 字符流的基本使用
public class IO2 {
public static void main(String[] args) throws IOException {
OutputStreamWriter isw = new OutputStreamWriter(new FileOutputStream("fos.txt"), "utf-8");
InputStreamReader isr = new InputStreamReader(new FileInputStream("fos.txt"), "utf-8");
// 与字节流相比,这里就可以直接将字符串写入文件
isw.write("字符流测试哇!");
isw.close();
// read方法会返回一个数字,每一个数字代表一个字符
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char) ch);
}
isr.close();
}
}
2.3 字符流的小技巧
public class IO4 {
public static void main(String[] args) throws IOException {
// 不涉及编码问题,可以使用FileWriter,FileReader。
FileWriter fw = new FileWriter("fos.txt");
FileReader fr = new FileReader("fos.txt");
for (int i = 0; i < 10; i++) {
fw.write("学好IO再学NIO\n");
fw.flush();
}
char[] ch = new char[1024];
int len;
while ((len=fr.read(ch))!=-1){
System.out.print(new String(ch, 0, len));
}
fw.close();
fr.close();
}
}
三、字符缓冲流
public class IO3 {
public static void main(String[] args) throws IOException {
// flush() 刷新流,还可以继续写数据
// close() 刷新,关闭流,释放资源
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("fos.txt", true), "utf-8"));
for (int i = 0; i < 10; i++) {
bw.write("洛天依");
bw.newLine();
bw.flush();
}
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fos.txt"), "utf-8"));
char[] ch = new char[1024];
int len;
while ((len=br.read(ch))!=-1){
System.out.println(new String(ch, 0, len));
}
br.close();
// readline 读取一行
// newline 使用系统的换行格式换行
}
}