IO基础(2)-IO缓冲流及其他流用法
来源:互联网 发布:java 邮箱匹配 编辑:程序博客网 时间:2024/04/19 15:31
一、缓冲流
- 为了提高数据读写的速度,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;/* * 抽象基类节点流(文件流) 缓冲流(处理流的一种,可以提升文件操作的效率) * InputStreamFileInputStreamBufferedInputStream * OutputStreamFileOutputStreamBufferedOutputStream (flush()) * ReaderFileReaderBufferedReader (readLine()) * WriterFileWriterBufferedWriter (flush()) */public class TestBuffered {@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();}}}}@Testpublic 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、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();}}}}//使用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();}}}}}
二、转换流
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”时,退出程序。 */@Testpublic 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 blocke.printStackTrace();}}}}/* * 如何实现字节流与字符流之间的转换: * 转换流:InputStreamReader OutputStreamWriter * 编码:字符串 --->字节数组 * 解码:字节数组--->字符串 */@Testpublic 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 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
- IO基础(2)-IO缓冲流及其他流用法
- IO流(二):字节流及其缓冲流
- IO流(三):字符流及其缓冲流
- IO流(字符流及其他问题)
- IO流(缓冲流)
- IO基础之缓冲流的详解
- java基础第十七天 io缓冲流
- IO之缓冲流
- java io 缓冲流
- IO缓冲流
- IO之缓冲流
- IO Streams:缓冲流
- IO基础(1)-File,IO流用法
- IO基础(3)-IO对象流用法
- 黑马程序员---java基础之IO(File类及其他流对象)
- 黑马程序员——IO流及其他常见流
- java 的常用类 io流 及其他
- IO流(5)—缓冲流
- 计算 -2
- Gym101194A-Number Theory Problem
- navicate连接oracle
- BZOJ 4950 Mission Improbable (二分图最大匹配)
- ida算法解析
- IO基础(2)-IO缓冲流及其他流用法
- 面试题(一)- 谈谈你对数据库中索引的理解
- mini版镜像Centos(无图形化界面)搭建集群环境(MAC地址冲突或者无效的解决方法)
- 机器学习 第六周 总结 知识点
- python matplotlib 入门系列三:坐标轴
- 【c基础知识】内存分区
- Mysql-mytop安装与使用
- Openjudge 2987 小兔子捡金币
- Discuz!X前台任意文件删除漏洞