处理流_缓冲流
2014-12-11 00:10
99 查看
1 和节点流相比,可以提升文件操作的效率
2 flush操作将最后缓存中剩余的字符串输出
2 flush操作将最后缓存中剩余的字符串输出
package lianxi1; 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; public class TestBuffered { @Test public void testCopy(){ long start = System.currentTimeMillis(); String src = "d:/io/plan.png"; String des = "prison.png"; copyFile(src,des); long end = System.currentTimeMillis(); System.out.println(end-start); } public void copyFile(String src,String des){ FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File file1 = new File(src); File file2 = new File(des); try{ fis = new FileInputStream(file1); fos = new FileOutputStream(file2); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); int len; byte[] b = new byte[100]; while((len=bis.read(b))!=-1){ bos.write(b, 0, len); bos.flush(); } }catch(Exception ex){ex.printStackTrace();} finally{ if(bis!=null){ try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bos!=null){ try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } @Test public void test2(){ FileReader fr = null; FileWriter fw = null; BufferedReader br = null; BufferedWriter bw = null; File file1 = new File("d:/io/hellotext.txt"); File file2 = new File("Tangshan"); try{ fr = new FileReader(file1); fw = new FileWriter(file2); br = new BufferedReader(fr); bw = new BufferedWriter(fw); // int len; // char[] c = new char[100]; // while((len=br.read(c))!=-1){ // bw.write(c, 0, len); // bw.flush(); // } //或用readline方法 String str; while((str=br.readLine())!=null){ bw.write(str); bw.newLine(); } }catch(Exception ex){ex.printStackTrace();} finally{ if(br!=null){ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bw!=null){ try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
相关文章推荐
- 今天终于明白了什么是缓冲流
- 黑马程序员_字符流_字节流_IO异常处理_文件的续写_拷贝文本_缓冲流
- 黑马程序员—字节流Stream和缓冲流Buffered的小练习
- 文件拷贝过程中使用文件流、缓冲流、转换流以及速率比较
- java缓冲流,BufferedReader,BufferedWriter 详解
- 【Java】javaIO之带缓冲流的字符流文件读写
- 字符缓冲流
- 包装流和缓冲流
- Java复习之IO缓冲流
- 字节流、字符流、字节缓冲流、字符缓冲流
- java 字符输入流,字符输出流,字符缓冲流
- IO流-BufferedInputStream输入缓冲流
- Java学习笔记40(缓冲流)
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
- java例程练习(缓冲流)
- IO流-输入输出流:字节流、字符流、缓冲流、转换流
- 缓冲流bufferedReader 与inputStream
- JAVA学习笔记(二十九) - 缓冲流 BufferedInputStream &BufferedOutputStream
- 黑马程序员_字节流、缓冲流
- 【JAVA开发之IO流1-3】IO流之缓冲流