java读取文件的方式和效率比较

1.按照字节读取
public String readFileByByte(String filePath){
long beginTime =System.currentTimeMillis();
StringBuffer stringBuffer=new StringBuffer();
byte[] buffer=new byte[2048];//现在一次读取2048字节,可以一次性读取文件的全部字节,不用循环读取了,这样是速度最快的
// Long filelength = file.length();
//byte[] filecontent = newbyte[filelength.intValue()];
//FileInputStream in = new FileInputStream(file);
// in.read(filecontent);
//return new String(filecontent, encoding);
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;
}
2.按字符读取
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;
}
3.按行读取
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;
}
总结:按照字节读取(一次读取文件全部字节最快),安照行读取最慢
public static String readToString(String fileName) {
String encoding = “UTF-8”;
System.out.println(System.currentTimeMillis());
File file = new File(fileName);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
System.out.println(System.currentTimeMillis());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
return new String(filecontent, encoding);
} catch (UnsupportedEncodingException e) {
System.err.println("The OS does not support " + encoding);
e.printStackTrace();
return null;
}
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值