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 包含一个内存缓冲区,字 ...
随机推荐
- [python] 查找列表中重复的元素
a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5] b = set(a) for each_b in b: count = 0 for each_a in a: if each_b ...
- 安卓——AppTheme
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=&q ...
- WDA基础八:ROWREPEATER的使用
这玩意不知道什么时候用^_^ 组件:Row-Repeater 1.新建WDA程序并激活 ZLYWDA02 2.进入VIEW,创建CONTEXT: 表:0..n 选择行:单选 初始化选择行 3.创建循 ...
- activiti实战系列之动态表单 formService 自定义变量类型
目前Activiti默认支持的类型有String,long,enum,date,boolean,collection 要自定义字段类型,首先需要表单类型解析类 /** * @Author:LJ * @ ...
- pandas报错处理:TypeError: Empty 'DataFrame': no numeric data to plot
Tushare返回的是pandas的DataFrame格式,但是执行以下代码时报错:TypeError: Empty 'DataFrame': no numeric data to plot impo ...
- linux使用lvresize和resize2fs调整lv大小
以下操作基于场景:有两个同vg的lv(applv和rootlv),我们需要从applv腾出1G给rootlv. 1.缩小applv磁盘 lvresize -L -1G /dev/mapper/myvg ...
- selinux介绍/状态查看/开启/关闭
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统--百度百科. 基于经验来说SELinux在 ...
- day_07_python_1124
01 昨日内容回顾 数据类型补充: str <---> list split join list <---> set set(list) list(set()) list &l ...
- java的八大排序
public class Sort2 { public static void main(String[] args) { Sort2 sort = new Sort2(); System.out.p ...
- 用c++写一个数据库
[cpp] view plain copy 第一步:构建一个头文件(**.h) [cpp] view plain copy #include<iostream> #include<i ...