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;
}
}
java读取文件的方式和效率比较
最新推荐文章于 2022-08-30 23:07:57 发布