IO加强之缓冲输入输出流测试
2017-04-01 18:19
323 查看
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.junit.Test;
//缓冲输入输出流的测试,通过以下三种情况来判断有没有buffer和buffer的位置对读取文件的影响
//1.不加缓存buffer
//2.将缓存buffer加在内部
//3.将缓存buffer加在外部
public class BuffereDemo {
@Test
public void demo1() throws IOException{
//不加缓冲输入输出流 读取结果14秒
long t1 = System.currentTimeMillis();//获取当前系统时间
DataInputStream din = new DataInputStream(
new FileInputStream("test.txt")
);
String str = null;
while((str = din.readLine())!=null){
System.out.println(str);
}
long t2 = System.currentTimeMillis();//获取当前系统时间
System.out.println("法1运行时间:"+(t2-t1));
}
@Test
//内部加缓存 读取结果 3秒
public void demo2() throws IOException{
long t1 = System.currentTimeMillis();
DataInputStream din = new DataInputStream(
new BufferedInputStream(
new FileInputStream("test.txt")
)
);
String str = null;
while((str = din.readLine())!=null){
System.out.println(str);
}
long t2 = System.currentTimeMillis();
System.out.println("法2运行时间:"+(t2-t1));
}
@Test
// 外部缓存 读取结果9秒
public void demo3() throws IOException{
long t1 = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(
new DataInputStream(
new FileInputStream("test.txt")
)
);
byte buf[] = new byte[30];
int len=0;
while( (len=bin.read(buf))!=-1 ){
System.out.println( new String(buf,0,len));
}
long t2 = System.currentTimeMillis();
System.out.println("法3运行时间:"+(t2-t1));
}
}
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.junit.Test;
//缓冲输入输出流的测试,通过以下三种情况来判断有没有buffer和buffer的位置对读取文件的影响
//1.不加缓存buffer
//2.将缓存buffer加在内部
//3.将缓存buffer加在外部
public class BuffereDemo {
@Test
public void demo1() throws IOException{
//不加缓冲输入输出流 读取结果14秒
long t1 = System.currentTimeMillis();//获取当前系统时间
DataInputStream din = new DataInputStream(
new FileInputStream("test.txt")
);
String str = null;
while((str = din.readLine())!=null){
System.out.println(str);
}
long t2 = System.currentTimeMillis();//获取当前系统时间
System.out.println("法1运行时间:"+(t2-t1));
}
@Test
//内部加缓存 读取结果 3秒
public void demo2() throws IOException{
long t1 = System.currentTimeMillis();
DataInputStream din = new DataInputStream(
new BufferedInputStream(
new FileInputStream("test.txt")
)
);
String str = null;
while((str = din.readLine())!=null){
System.out.println(str);
}
long t2 = System.currentTimeMillis();
System.out.println("法2运行时间:"+(t2-t1));
}
@Test
// 外部缓存 读取结果9秒
public void demo3() throws IOException{
long t1 = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(
new DataInputStream(
new FileInputStream("test.txt")
)
);
byte buf[] = new byte[30];
int len=0;
while( (len=bin.read(buf))!=-1 ){
System.out.println( new String(buf,0,len));
}
long t2 = System.currentTimeMillis();
System.out.println("法3运行时间:"+(t2-t1));
}
}
相关文章推荐
- IO(2) 缓冲字节输入输出流
- java IO-输入输出流(三)
- java IO-输入输出流(五)
- java Io总结一(输入输出流)
- 第十四篇 黑马程序员-IO输入输出流
- 第十二篇 黑马程序员-IO输入输出流
- java IO-输入输出流(一)
- IO操作 改变标准输入输出流和转换流练习
- Java IO 输入输出流的理解 1
- Java IO之有缓冲的文本输入
- 传智播客——IO:输入输出流
- 文件输入输出流->带缓冲的输入输出流(过滤流)->基本数据的输入输出流 链接 增强了文件输入输出流的功能
- 10.31 流 IO 输入输出流
- IO输入/输出流的简单总结
- C#操作IO(输入输出流)
- java IO-输入输出流(四)
- 字符串输入输出流:java.io.BufferedReader和java.io.BufferedWriter
- 文件输入输出流->带缓冲的输入输出流(过滤流)->基本数据的输入输出流 链接 增强了文件输入输出流的功能
- Java IO之有缓冲的文本输入
- IO输入输出流的学习笔记