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 包含一个内存缓冲区,字 ...
随机推荐
- hbase知识
HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统 HBASE的目标是存储并处理大型的数据,更具体来说是仅需使用普通的硬件配置,就能够处理由成千上万的行和列所组成的大型数据. HBASE ...
- hdu-6435
Problem J. CSGO Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others ...
- InnoDB存储引擎介绍-(1)InnoDB存储引擎结构
首先以一张图简单展示 InnoDB 的存储引擎的体系架构. 从图中可见, InnoDB 存储引擎有多个内存块,这些内存块组成了一个大的内存池,主要负责如下工作: 维护所有进程/线程需要访问的多个内部数 ...
- HTML5新特性 Web Workers 实现多线程
引子:(JS单线程) 什么是webworker? Web Worker为Web应用程序提供了一种能在后台中运行的方法.通过Web Worker可以生成多个线程同时运行,并保证页面对用户的及时响应,完全 ...
- Java 8 默认方法(Default Methods)
Java 8 默认方法(Default Methods) Posted by Ebn Zhang on December 20, 2015 Java 8 引入了新的语言特性——默认方法(Default ...
- 把旧系统迁移到.Net Core 2.0 日记(6) MapRoute/Area/ViewPath
我想实现 http://localhost:5000/{moduleName}/{controller}/{action}/{id?} 这样的url. 有2个方法 方法1: 在路由里设置多个modul ...
- Node.js是用来干嘛的
如果你去年注意过技术方面的新闻,我敢说你至少看到node.js不下一两次.那么问题来了“node.js是什么?”.有些人没准会告诉你“这是一种通过JavaScript语言开发web服务端的东西”.如果 ...
- window7下载安装桌面版ubuntu
首先需要下载VMware Workstation 下载地址:http://pan.baidu.com/s/1qXS0rhi 秘钥:bbpn 我的环境是ubuntu-14.10-desktop-a ...
- javascript void函数
<a href="javascript:doTest2();void(0);">here</a> 但这儿的void(0)究竟是何含义呢? Javascrip ...
- day03_python_1124
01 昨日内容回顾 while 条件: 循环体 如何终止循环: 1,改变条件. 2,break. 3,exit() quit() 不推荐. 关键字: break continue while else ...