Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems during counting strings in the txt file
    primarykey
    data
    text
    <p>I am developing a progam which reads a text file and creates a report. The content of the report is the following: the number of every string in file, its "status", and some symbols of every string beginning. It works well with file up to 100 Mb. </p> <p>But when I run the program with input files which are bigger than 1,5Gb in size and contain more than 100000 lines, I get the following error:</p> <pre><code>&gt; Exception in thread "main" java.lang.OutOfMemoryError: Java heap space &gt; at java.util.Arrays.copyOfRange(Unknown Source) at &gt; java.lang.String.&lt;init&gt;(Unknown Source) at &gt; java.lang.StringBuffer.toString(Unknown Source) at &gt; java.io.BufferedReader.readLine(Unknown Source) at &gt; java.io.BufferedReader.readLine(Unknown Source) at &gt; org.apache.commons.io.IOUtils.readLines(IOUtils.java:771) at &gt; org.apache.commons.io.IOUtils.readLines(IOUtils.java:723) at &gt; org.apache.commons.io.IOUtils.readLines(IOUtils.java:745) at &gt; org.apache.commons.io.FileUtils.readLines(FileUtils.java:1512) at &gt; org.apache.commons.io.FileUtils.readLines(FileUtils.java:1528) at &gt; org.apache.commons.io.ReadFileToListSample.main(ReadFileToListSample.java:43) </code></pre> <p>I increased VM arguments up to -Xms128m -Xmx1600m (in eclipse run configuration) but this did not help. Specialists from OTN forum advised me to read some books and improve my program's performance. Could anybody help me to improve it? Thank you.</p> <p>code:</p> <pre><code>import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.PrintStream; import java.util.List; public class ReadFileToList { public static void main(String[] args) throws FileNotFoundException { File file_out = new File ("D:\\Docs\\test_out.txt"); FileOutputStream fos = new FileOutputStream(file_out); PrintStream ps = new PrintStream (fos); System.setOut (ps); // Create a file object File file = new File("D:\\Docs\\test_in.txt"); FileReader fr = null; LineNumberReader lnr = null; try { // Here we read a file, sample.txt, using FileUtils // class of commons-io. Using FileUtils.readLines() // we can read file content line by line and return // the result as a List of string. List&lt;String&gt; contents = FileUtils.readLines(file); // // Iterate the result to print each line of the file. fr = new FileReader(file); lnr = new LineNumberReader(fr); for (String line : contents) { String begin_line = line.substring(0, 38); // return 38 chars from the string String begin_line_without_null = begin_line.replace("\u0000", " "); String begin_line_without_null_spaces = begin_line_without_null.replaceAll(" +", " "); int stringlenght = line.length(); line = lnr.readLine(); int line_num = lnr.getLineNumber(); String status; // some correct length for if int c_u_length_f = 12; int c_ea_length_f = 13; int c_a_length_f = 2130; int c_u_length_e = 3430; int c_ea_length_e = 1331; int c_a_length_e = 442; int h_ext = 6; int t_ext = 6; if ( stringlenght == c_u_length_f || stringlenght == c_ea_length_f || stringlenght == c_a_length_f || stringlenght == c_u_length_e || stringlenght == c_ea_length_e || stringlenght == c_a_length_e || stringlenght == h_ext || stringlenght == t_ext) status = "ok"; else status = "fail"; System.out.println(+ line_num + stringlenght + status + begin_line_without_null_spaces); } } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>Also specialists from OTN said that this programm opens the input and reading it twice. May be some mistakes in "for statement"? But I can't find it. Thank you.</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.
 

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