一、缓冲流
-
为了提高数据读写的速度, JavaAPI 提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组
-
根据数据操作单位可以把缓冲流分为:
BufferedInputStream和 BufferedOutputStream
BufferedReader和 BufferedWriter
-
缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法
-
对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
/*
* 抽象基类 节点流(文件流) 缓冲流(处理流的一种,可以提升文件操作的效率)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream (flush())
* Reader FileReader BufferedReader (readLine())
* Writer FileWriter BufferedWriter (flush())
*/
public class TestBuffered {
@Test
public void testBufferedReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("dbcp.txt");
File file1 = new File("dbcp3.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while((len = br.read(c))!= -1){
// String str = new String(c, 0, len);
// System.out.print(str);
// }
String str;
while((str = br.readLine()) != null){
// System.out.println(str);
bw.write(str + "\n");
// bw.newLine();
bw.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
// String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
// String dest = "C:\\Users\\shkstart\\Desktop\\3.avi";
String src = "C:\\Users\\shkstart\\Desktop\\实验.doc";
String dest = "C:\\Users\\shkstart\\Desktop\\实验1.doc";
copyFile(src,dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//746
}
//使用缓冲流实现文件的复制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入、写出的文件
File file1 = new File("1.jpg");
File file2 = new File("2.jpg");
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
二、转换流
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
public class TestOtherStream {
/*
* 标准的输入输出流:
* 标准的输出流:System.out
* 标准的输入流:System.in
*
* 题目:
* 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,
* 直至当输入“e”或者“exit”时,退出程序。
*/
@Test
public void test2(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while(true){
System.out.println("请输入字符串:");
str = br.readLine();
if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
break;
}
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 如何实现字节流与字符流之间的转换:
* 转换流:InputStreamReader OutputStreamWriter
* 编码:字符串 --->字节数组
* 解码:字节数组--->字符串
*/
@Test
public void test1(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//解码
File file = new File("dbcp.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);
//编码
File file1 = new File("dbcp4.txt");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}