目录
一、缓冲流
概念:
缓冲流属于处理流,即套接在已有流的基础上的流。
作用:
可以加快流的读取和写入。因为内部提供了一个8192byte/char的缓冲区。
分类:
字节缓冲流和字符缓冲流。
代码:
package IOStream;
import org.junit.Test;
import java.io.*;
//缓冲流
//属于处理流,就是套接在已有的流上面的流,加快流的读取和写入的速度
//提高读写速度的原因:内部提供了一个8192byte的缓冲区
public class demo4 {
public static void main(String[] args) {
}
@Test
//缓冲字节流处理非文本文件
public void Buffer_in_out () {
BufferedInputStream bi= null;
BufferedOutputStream bo= null;
try {
File f1=new File("D:\\Java develop\\Program\\superior_Java\\ingram.jpg");
File f2=new File("D:\\Java develop\\Program\\superior_Java\\ingram3.jpg");
FileInputStream fi=new FileInputStream(f1);
bi = new BufferedInputStream(fi);
FileOutputStream fo=new FileOutputStream(f2);
bo = new BufferedOutputStream(fo);
byte []arr=new byte[10];
int len;
while ((len=bi.read(arr))!=-1){
bo.write(arr,0,len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bi != null) {
try {
bi.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (bo != null){
try {
bo.close();
} catch (IOException e) {
throw new RuntimeException(e);
}}
}
}
//字符缓冲流
//方式一
@Test
public void Buffer_read_write() {
BufferedReader br= null;
BufferedWriter bw= null;
try {
File f1=new File("D:\\Java develop\\Program\\superior_Java\\hello1.txt");
File f2=new File("D:\\Java develop\\Program\\superior_Java\\hello3.txt");
FileReader fr=new FileReader(f1);
FileWriter fw=new FileWriter(f2);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char []arr=new char[10];
int len;
while ((len= br.read(arr))!=-1){
bw.write(arr,0,len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (bw!=null)
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
//字符缓冲流
//方式二
@Test
public void Buffer_read_write2() {
BufferedReader br= null;
BufferedWriter bw= null;
try {
File f1=new File("D:\\Java develop\\Program\\superior_Java\\hello1.txt");
File f2=new File("D:\\Java develop\\Program\\superior_Java\\hello4.txt");
FileReader fr=new FileReader(f1);
FileWriter fw=new FileWriter(f2);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String data;
while ((data= br.readLine())!=null){
bw.write(data);
bw.newLine();//换行作用
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (bw!=null)
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
二、转换流
概念:
转换流提供了字节流和字符流之间的转换。
类:
InputStreamReader:字节流转字符流输入
OutputStreamWriter:字符流转字节流输出
代码:
如下代码将一个文本文件以UTF-8的格式读入,然后以gbk的格式写入另一个文件。
package IOStream;
import org.junit.Test;
import java.io.*;
//转换流
//转换流提供了字节流和字符流之间的转换
//InputStreamReader
//OutputStreamWriter
public class demo5 {
public static void main(String[] args) {
}
@Test
public void test_InputStreamReader() throws IOException {
FileInputStream fi=new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello1.txt");
InputStreamReader ir=new InputStreamReader(fi,"UTF-8");
FileOutputStream fo=new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello5.txt");
OutputStreamWriter ow=new OutputStreamWriter(fo,"gbk");
int len;
char[]buf=new char[5];
while ((len= ir.read(buf))!=-1){
ow.write(buf,0,len);
}
ow.close();
ir.close();
}
}