10040---Java IO --缓冲流
来源:互联网 发布:淘宝 服务器品牌 编辑:程序博客网 时间:2024/05/19 14:40
//使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制@Testpublic void testBufferedInputOutputStream(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//1.提供读入、写出的文件File file1 = new File("1.jpg");File file2 = new File("2.jpg");//2.想创建相应的节点流:FileInputStream、FileOutputStreamFileInputStream 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 blocke.printStackTrace();}finally{//5.关闭相应的流if(bos != null){try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
---
bos.flush();
---
缓冲流(处理流的一种,可以提升文件操作的效率)
BufferedInputStream
BufferedOutputStream (flush())
BufferedReader (readLine())
BufferedWriter (flush())
实际的开发中,用的是BufferedXXX类,不用FileXXX,因为前者效率高。
----
@Testpublic 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 blocke.printStackTrace();}finally{if(bw != null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(br != null){try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
0 0
- 10040---Java IO --缓冲流
- java io 缓冲流
- java IO流-缓冲流
- JAVA Io 缓冲输入输出流
- 【java的 IO流之缓冲流】
- java---IO流缓冲输入输出流
- Java IO流之缓冲流
- JAVA IO之缓冲流源码分析
- java Io 缓冲流 BufferedInputStream BufferedOutputStream 笔记
- java Io 缓冲流 BufferedReader BufferedWriter 笔记
- java.se.io.7.缓冲处理流
- Java复习之IO缓冲流
- java IO(输入输出) 字节缓冲流
- java基础第十七天 io缓冲流
- Java IO ---学习笔记(缓冲流)
- IO之缓冲流
- IO缓冲流
- IO之缓冲流
- IPC机制(跨进程通信)
- JMC 之 内存篇
- IntentFilter的使用
- Linux内核模块
- Android的启动模式
- 10040---Java IO --缓冲流
- HDOJ/HDU 2564 词组缩写(单词缩写)
- 6/05/2016函数模板
- 算法_15:数值算法
- <<离散数学>>树就是一种计算方式
- C++类使用注意规则
- 更安全的MmIsAddressValid
- Codeblocks中的empty project和console application
- 如何查找dev目录下磁盘设备的iscsi来源