Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public String readFile() { File f = new File("/Users/Guest/Documents/workspace/Project/src/test.txt"); FileReader fr = null; BufferedReader br = null; StringBuilder content = new StringBuilder();; try { fr = new FileReader(f); br = new BufferedReader(fr); //int c; //while ((c = fr.read()) != -1) { //content.append((char) c); //} String line = null; while((line = br.readLine()) != null) { content.append(line); } fr.close(); br.close(); } catch (Exception e) { // do something } return content.toString(); } </code></pre> <p>Use buffered reader and youll get 70%+ improvement, use string builder instead of string buffer unless you need syncronization.</p> <p>ran it on a 10MB file 50 times and averaged </p> <ul> <li>no need to put anything that does not need exception handling inside try</li> <li>no need for that if clause because it will be true only once and so you're wasting time - checking it for every character</li> <li>there is no runtime exceptions to throw.</li> </ul> <p>results: fastest combination to slowest:</p> <ol> <li>string builder and buffered reader line by line: 211 ms </li> <li>string buffer and buffered reader line by line: 213 ms </li> <li>string builder and buffered reader char by char: 348 ms </li> <li>string buffer and buffered reader char by char: 372 ms </li> <li>string builder and file reader char by char: 878 </li> <li>string buffer and file reader char by char: 935 ms</li> <li>string: extremely slow</li> </ol> <p>so use string builder + buffered reader and make it read line by line for best results.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload