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

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

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

最新推荐文章于 2024-09-13 00:43:46 发布

原创

最新推荐文章于 2024-09-13 00:43:46 发布

·

置顶

·

3.6k 阅读

·

3

·

3

·

CC 4.0 BY-SA版权

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

文章标签:

#文件读取方式

#文件读取效率

java

专栏收录该内容

4 篇文章

订阅专栏

博客介绍了三种文件读取方式,包括按字节、按字符和按行读取,并给出了相应的代码示例。通过代码中的计时操作可对比不同方式的效率,总结指出按字节一次读取文件全部字节速度最快,按行读取最慢。

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;

}

}

风雨相关