java 按字节读入_Java按字节读取文件
【题外话】今天学习了Object-c。通过看书发现,Object-c跟大多数面向对象的语言一样,是很有意思的语言。里面的语法跟Java还有C++,形式上有些差异。至于我为什么要学OC,这个是因为工作需要。OC有什么独有的特点,我现在还没有特别深刻的体会。现在看起来,OC就是表达形式有个性。
Java按字节读取文件。
Java将文件读入内存中有几种方式:
1.按照字节读取
2.安照字符读取
3.按照行读取
4.随机读取
今天我就讲一下按照字节读取。
没有公交车了,以后继续。
2015/3/26
继续我们昨天的话题,现在接着聊。
Java程序读取文件到内存中。
上述的一句话是一个陈述语言,表达的意思是:使用Java程序语言,将计算机中的一个文件,读入内存中。
在实现上述目的之前,我先说明一下我的思维方式:
1.我要干什么?
2.有多少种方案?
3.哪个或者哪几个方案是最好的?
4.记录
首先,我分析一下,我要干什么?
我的目的很明确,使用Java语言,将计算机中的一个文件,读入内存中。
这时,在我的思维里面有两种方案:
1.按照字节读入内存
2.安照字符读入内存
这两个方案,在某种层次上来说,是具有原子性的,他们可再分。
我先选择第一个方案:
下面是执行的代码:按字节读入
public String readFileByByte(String filePath){
long beginTime =System.currentTimeMillis();
StringBuffer stringBuffer=new StringBuffer();
byte[] buffer=new byte[2048];
int count=0;
File file=new File(filePath);
try{
InputStream inputStream=new FileInputStream(file);
while(-1!=(count=inputStream.read(buffer))){
stringBuffer.append(new String(buffer,0,count));
}
inputStream.close();
}catch(IOException ex){
ex.printStackTrace();
}
long endTime =System.currentTimeMillis();
System.out.println(this.getClass().getName()+",readFileByByte method costs :"+(endTime-beginTime));
System.out.println(this.getClass().getName()+",readFileByByte size :"+(stringBuffer.toString().length()));
return null;
}
单元测试,其执行时间为:(10次)
1.429 毫秒 2.557 毫秒 3.537 毫秒 4. 672毫秒 5.597毫秒 6.568毫秒 7.538 毫秒
8.570毫秒 9.562毫秒 10.557毫秒
平均执行时间:558.7 毫秒
第二个方案:按字符读入
public String readFileByChar(String filePath){
long beginTime =System.currentTimeMillis();
char[] buffer=new char[1024];
StringBuffer stringBuffer=new StringBuffer();
int count=0;
File file=new File(filePath);
try{
FileReader inFile=new FileReader(file);
while(-1!=(count=inFile.read(buffer))){
stringBuffer.append(new String(buffer,0,count));
}
inFile.close();
}catch(IOException ex){
ex.printStackTrace();
}
long endTime =System.currentTimeMillis();
System.out.println(this.getClass().getName()+",readFileByChar method costs :"+(endTime-beginTime));
System.out.println(this.getClass().getName()+",readFileByChar size :"+(stringBuffer.toString().length()));
return null;
}
单元测试,其执行时间为:(10次)
1.481 毫秒 2.458 毫秒 3.459 毫秒 4. 460毫秒 5.436毫秒 6.491毫秒 7.556 毫秒
8.463毫秒 9.462毫秒 10.485毫秒
平均执行时间:475.1 毫秒 这是按字符读入,添加缓存方式:
public String readFileByCharUseBuffer(String filePath){
long beginTime =System.currentTimeMillis();
File file=new File(filePath);
String string;
StringBuffer stringBuffer=new StringBuffer();
try{
FileReader fileReader=new FileReader(file);
BufferedReader buffedReader=new BufferedReader(fileReader);
while(null!=(string=buffedReader.readLine())){
stringBuffer.append(string);
}
buffedReader.close();
}catch(IOException ex){
ex.printStackTrace();
}
long endTime =System.currentTimeMillis();
System.out.println(this.getClass().getName()+",readFileByCharUseBuffer method costs :"+(endTime-beginTime));
System.out.println(this.getClass().getName()+",readFileByCharUseBuffer size :"+(stringBuffer.toString().length()));
return null;
}
单元测试,其执行时间为:(10次)
1.641 毫秒 2.626 毫秒 3.692 毫秒 4. 610毫秒 5.612毫秒 6.653毫秒 7.614 毫秒
8.586毫秒 9.588毫秒 10.818毫秒
平均执行时间:644 毫秒
上述读取的文件大小是:58511KB
第一种方式的平均执行时间为:558.7毫秒
第二种方式的平均执行时间为:475.1毫秒
第三种方式的平均执行时间为:644 毫秒
现在,增大读取文件的大小,第二次测试文件大小是:480816KB(都是实验十次,取平均值)
第一种方式的平均执行时间为:3205 毫秒
第二种方式的平均执行时间为:2911毫秒
第三种方式的平均执行时间为:3511毫秒
通过上述的测试:按字符读取的速度最快,而按照字符读取,同时使用缓存的方式最慢。通过比较发现,按照字符读取,速度就是快。
那么,这个问题的描述就是,Java语言,将本地文件以字符的方式读入内存,是最快的。
2015-3-30
我对之前的总结有一些疑问,这个疑问并不是java读取文件的疑问,而是BufferedReader。按理来说,我使用的是带缓存的IO,也就是说输入进来的数据会有缓存,应该比其他的方式快,但是上述的测试结果,并不是这样的,为什么?
我先搜索了一下CPU从硬盘上读取数据的顺序:
(信息来源于百度知道)
CPU先是从自己带的一级缓存里找数据,还就是二级缓存,下面就是三级缓存了(如果有的话),接下来就是内存,再下面就是硬盘的虚拟内存,再再接就就是正常的硬盘分区了,怎么处理的。。。。01001001010010100100010001001
既然是这样,那BufferedReader和FileReader有何区别?BufferedReader的缓冲优势体现在那个地方?好困惑。突然想起萧煌奇唱的
你是我的眼,我觉得我虽然有双健康的双眼,但是,在这个问题上,有什么东西遮住了他,让我看到一片荒芜。