java学习笔记30(IO :缓冲流)
缓冲流:
读取数据大量的文件时,读取的速度慢,java提供了一套缓冲流,提高IO流的效率;
缓冲流分为字节缓冲流和字符缓冲流;
字节输入缓冲流和字节输出缓冲流如下:
package com.zs.Demo; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Demo {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字节缓冲输入流
private static void fun1() throws IOException {
//第二种创建缓冲流方式
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\\a.txt"));
int len=0;
while((len=bis.read())!=-1){
System.out.println((char)len);
}
bis.close();
}
//字节缓冲输出流
private static void fun() throws IOException {
//第一种方式
FileOutputStream f=new FileOutputStream("d:\\a.txt");
BufferedOutputStream bos=new BufferedOutputStream(f);
//写入一个字节
bos.write(105);
bos.write("hello world".getBytes());//写入字节数组
bos.write("hello world".getBytes(),0,2);//写入字节数组指定内容
bos.close();
}
}
字符输入缓冲流和输出缓冲流如下:
package com.zs.Demo; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Demo2 {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字符输出缓冲流
private static void fun() throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\b.txt"));
bw.write(111);//自动查码表,编码
bw.flush();//注意字符输出流每次操作都要刷新
bw.write("hello world".toCharArray());//写入字符数组
bw.flush();
bw.newLine();//newLine()特有方法,换行
bw.flush();
bw.write("java");//写入字符串
bw.flush();
bw.close();
}
//字符输入缓冲流
private static void fun1() throws IOException {
//字符输入缓冲流
BufferedReader br=new BufferedReader(new FileReader("d:\\b.txt"));
int len=0;
while ((len=br.read())!=-1) {
System.out.print((char)len);
}
br.close(); //创建字符数组输入缓冲流对象
BufferedReader br1=new BufferedReader(new FileReader("d:\\b.txt"));
int len1=0;
char[] c=new char[1024];
while((len1=br1.read(c))!=-1){
System.out.print(new String(c,0,len1));
}
br1.close(); //字符输入缓冲流特有的方法readLine() 一次读取一行
BufferedReader br2=new BufferedReader(new FileReader("d:\\b.txt"));
String len3=null;//这里注意是字符串类型
while((len3=br2.readLine())!=null){
System.out.println(len3);
}
br2.close();
}
}
下面写一个比较字节流,字节数组流,字节缓冲流,字节数组缓冲流复制文件速度的代码:
package com.zs.Demo;
import java.io.*;
public class Demo5 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
//这里一个一个方法的运行,比较时间,复制的文件是一个240M的视频文件
// fun1("h:\\1.mp4","g:\\1.mp4");//字节流
// fun2("h:\\1.mp4","g:\\1.mp4");//字节数组流
// fun3("h:\\1.mp4","g:\\1.mp4");//字节缓冲流
fun4("h:\\1.mp4","g:\\1.mp4");//字节数组缓冲流
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
//字节数组缓冲流
private static void fun4(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
byte[] b=new byte[1024*10];
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//1653毫秒
fo.close();
fi.close();
}
//字节缓冲流
private static void fun3(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//13015毫秒
fo.close();
fi.close();
}
//字节数组流
private static void fun2(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
byte[] b=new byte[1024*10];
int len=0;
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//6979毫秒
fo.close();
fi.close();
}
//字节流
private static void fun1(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//太慢了,等不下去了,时间十分钟以上
fo.close();
fi.close();
}
}
可以看出字节数组缓冲流速度最快,
java学习笔记30(IO :缓冲流)的更多相关文章
- Java学习笔记40(缓冲流)
缓冲流: 在读写文件的各种流中,最令人烦恼的就是效率问题, 而缓冲流的目的就是提高读写效率 字节输出缓冲流: package demo; import java.io.BufferedOutputSt ...
- java学习笔记之IO编程—内存流、管道流、随机流
1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...
- Java分享笔记:使用缓冲流复制文件
[1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...
- 【原】Java学习笔记033 - IO
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:继承关系中爷 ...
- Java学习笔记之——IO
一. IO IO读写 流分类: 按照方向:输入流(读),输出流(写) 按照数据单位:字节流(传输时以字节为单位),字符流(传输时以字符为单位) 按照功能:节点流,过滤流 四个抽象类: InputStr ...
- java学习笔记之IO编程—打印流和BufferedReader
1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...
- Java学习笔记43(打印流、IO流工具类简单介绍)
打印流: 有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器 PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(Outpu ...
- java学习笔记之IO编程—字节流和字符流
1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...
- Java学习笔记-10.io流
1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...
随机推荐
- Spring Boot常用注解
SpringBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enable ...
- H5 DeviceMotionEvent 事件制作“摇一摇效果”
摇一摇”的效果制作主要依赖于H5的deviceMotionEvent事件 先讲怎么使用,具体的原理在后边补充 第一步:捕捉重力加速度 var acceleration = eventData.acce ...
- 如何使a标签打开新页面并阻止刷新当前页面
错误: HTML中,使用href属性时,当前页面和新页面均跳转到URL指定的页面,即当前页面也刷新: <li id='goToBack'><a href='**.action' ta ...
- Git:多人推送/抓取分支事项
1.推送分支 1.1使用命令符git push origin branch-name,推送自己已修改的分支 例如git push origin master,git push origin dev. ...
- Python条件判断和循环,range()函数
条件判断经常使用if语句进行判断,表达方式为:if 条件语句: :elif:else if...用于执行第一条不满足if的判断,继续执行其它的判断.比如一个简单的if判断 Python3取消 ...
- 谈一谈JUnit神奇的报错 java.lang.Exception:No tests found matching
最近在学习Spring+SpringMVC+MyBatis,一个人的挖掘过程确实有点艰难,尤其是有一些神奇的报错让你会很蛋疼.特别是接触一些框架还是最新版本的时候,会因为版本问题出现很多错误,欢迎大家 ...
- 微信支付 php兼容问题
总结: php7 已删除 HTTP_RAW_POST_DATA 获取时需要file_get_contents("php://input"); 下面的是兼容方法. //存储微信的回 ...
- win7下使用U盘安装双系统(Ubuntu-17)
1.首先下载Ubuntu镜像文件,下载地址:http://mirrors.neusoft.edu.cn/ 2.下载 U盘操作系统安装工具- Universal USB Installer ,下载地址: ...
- laravel composer 安装指定版本以及基本的配置
1 安装指定的 laravel版本 以下的案例是安装5.2版本 composer create-project laravel/laravel=5.2.* --prefer-dist 2 配置 优化相 ...
- shell脚本分析一
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界 ...