Improving performance of reading text from a large text file

来源:互联网 发布:贪心算法例题 编辑:程序博客网 时间:2024/04/29 09:41

KEYNOTE: Using containers such as StringBuilder and ArrayList<String> instead of String.

Before:

try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"));String str = "";String content = "";int counter = 0;while ((str = reader.readLine()) != null) {content += str + "\n";counter ++;if(counter % 1000 == 0)System.out.println(counter);}         reader.close();} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("[STEP1] - file to open the file.");}


After improving performance:

File f = new File(filename);String fileContent = "";StringBuilder builder = new StringBuilder();ArrayList<> model = new ArrayList<>();try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"));String str = "";int counter = 0;while ((str = reader.readLine()) != null) {// NOTE: String might reduce performancebuilder.append(str);counter ++;if(counter % 1000 == 0)System.out.println(counter);}         reader.close();} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("[STEP1] - file to open the file.");}


0 0