Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since your code has performance issues, you first need to find bottle neck. You can profile it with profiler available with IDE you use.</p> <p>However since your code is not high in computation but IO intensive, both in reading file and output using System.out.print, that is where I would suggest you to improve on for improving on file IO.</p> <p>.</p> <p>Replace this line of code</p> <pre><code>Scanner scanner = new Scanner(new File("i:\\1\\2.txt")); </code></pre> <p>.</p> <p>With this lines of code</p> <pre><code>File file = new File("i:\\1\\2.txt"); BufferedReader br = new BufferedReader( new FileReader(file) ); Scanner scanner = new Scanner(br); </code></pre> <p>Let us know if this helps.</p> <p>.</p> <p>Since previous solution did not helped much, I made few more changes to improve your code. You may have to correct errors in parsing if any. I was able to display output of parsing 392832 lines in approx 5 seconds. Original solution takes more than 50 seconds.</p> <p><strong>Chages are as below:</strong></p> <ol> <li>Use of StringTokenizer instead of Scanner</li> <li>Use of BufferedReader for reading file</li> <li>Use of StringBuilder to buffer output</li> </ol> <p>.</p> <pre><code>public class FileParse { private static final int FLUSH_LIMIT = 1024 * 1024; private static StringBuilder outputBuffer = new StringBuilder( FLUSH_LIMIT + 1024); private static final long countCellId; public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); String fileName = "i:\\1\\2.txt"; File file = new File(fileName); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ";|, "); while (st.hasMoreTokens()) { String token = st.nextToken(); processToken(token); } } flushOutputBuffer(); System.out.println("----------------------------"); System.out.println("CELLID Count: " + countCellId); long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start)); } private static void processToken(String token) { if (token.startsWith("CELLID=")) { String value = getTokenValue(token); outputBuffer.append("CELLID:").append(value).append("\n"); countCellId++; } else if (token.startsWith("ENSUP=")) { String value = getTokenValue(token); outputBuffer.append("ENSUP:").append(value).append("\n"); } else if (token.startsWith("ENCHO=")) { String value = getTokenValue(token); outputBuffer.append("ENCHO:").append(value).append("\n"); } if (outputBuffer.length() &gt; FLUSH_LIMIT) { flushOutputBuffer(); } } private static String getTokenValue(String token) { int start = token.indexOf('=') + 1; int end = token.length(); String value = token.substring(start, end); return value; } private static void flushOutputBuffer() { System.out.print(outputBuffer); outputBuffer = new StringBuilder(FLUSH_LIMIT + 1024); } } </code></pre> <p>.</p> <p><strong>Update on ENSUP and MSLH:</strong></p> <p>To me it looks like you have switched ENSUP and MSLH in if statement as below. Hence you see "MSLH" value for "ENSUP" and vice a versa.</p> <pre><code>} else if (token.startsWith("MSLH=")) { String value = getTokenValue(token); outputBuffer.append("ENSUP:").append(value).append("\n"); } else if (token.startsWith("ENSUP=")) { String value = getTokenValue(token); outputBuffer.append("MSLH:").append(value).append("\n"); } </code></pre>
 

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