Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest java.io.LineNumberReader. It extends BufferedReader and you can use its <code>LineNumberReader.getLineNumber();</code> to get the current line number</p> <p>You can also use Java 7 <code>java.nio.file.Files.readAllLines</code> which returns a <code>List&lt;String&gt;</code> if it suits you better</p> <p>Note: </p> <p>1) favour StringBuilder over StringBuffer, StringBuffer is just a legacy class</p> <p>2) <code>contents.append(System.getProperty("line.separator"))</code> does not look nice use <code>contents.append(File.separator)</code> instead</p> <p>3) Catching exception seems irrelevant, I would also suggest to change your code as </p> <pre><code>public static String getContents(File aFile) throws IOException { BufferedReader rdr = new BufferedReader(new FileReader("aFile")); try { StringBuilder sb = new StringBuilder(); // read your lines return sb.toString(); } finally { rdr.close(); } } </code></pre> <p>now code looks cleaner in my view. And if you are in Java 7 use try-with-resources</p> <pre><code> try (BufferedReader rdr = new BufferedReader(new FileReader("aFile"))) { StringBuilder sb = new StringBuilder(); // read your lines return sb.toString(); } </code></pre> <p>so finally your code could look like </p> <pre><code>public static String[] getContents(File aFile) throws IOException { try (LineNumberReader rdr = new LineNumberReader(new FileReader(aFile))) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (String line = null; (line = rdr.readLine()) != null;) { if (rdr.getLineNumber() &gt;= 1500) { sb2.append(line).append(File.pathSeparatorChar); } else if (rdr.getLineNumber() &gt; 500) { sb1.append(line).append(File.pathSeparatorChar); } } return new String[] { sb1.toString(), sb2.toString() }; } } </code></pre> <p>Note that it returns 2 strings 500-1499 and 1500-2000</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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