14 Replies Latest reply: Mar 23, 2012 5:13 AM by 902483 RSS

    Get strings lenght from txt file and put string num and its lenght in log

    902483
      Hello. I am a newbie in Java. Could you help to write a programm which looks into txt file, calculates the lenght of every string from the file and log the results?

      For example, there is test.txt file located on d:\temp\test.txt
      Is consists of:
      122
      61263
      54545 45
      1

      I need to receive d:\temp\test_log.txt which include:
      1 string lenght = 3
      2 string lenght = 5
      3 string lenght = 8
      4 string lenght = 1

      My problem is that i have not enough experience in Java and I just know the parts of this programm:

      1. I can read file line by line:
      import java.io.*;
      class FileRead
      {
      public static void main(String args[])
           {
      try{
                FileInputStream fstream = new FileInputStream("d:\\temp\\test.txt");
                DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)      {
                     // Print the content on the console
                     System.out.println (strLine);
                }
                //Close the input stream
                in.close();
                }catch (Exception e){//Catch exception if any
                     System.err.println("Error: " + e.getMessage());
                }
           }
      }

      2. Calculate string lenght:

      public class StringLengthExample {
           public static void main(String[] args) {
           String string = "Count ";
           int stringlength = string.length();
           System.out.println("String lenght = "+stringlength);
           }
      }


      3. and write the text to file:

      public class RedirectSystemOut {
           public static void main(String[] args) throws FileNotFoundException {
                File file = new File("d:\\temp\\test_out.txt");
                FileOutputStream fos = new FileOutputStream(file);
                PrintStream ps = new PrintStream(fos);
                System.setOut(ps);
                System.out.println("Test record");
           }
      }

      Could anybody help me to connect these parts into one programm which reads txt file string by string and creates a log with lenght for every string?
      Or just give me an advise. Thank you.
        • 1. Re: Get strings lenght from txt file and put string num and its lenght in log
          gimbal2
          Be honest: this is a homework assignment, isn't it?
          • 2. Re: Get strings lenght from txt file and put string num and its lenght in log
            902483
            no, really. i need this to control some process.
            • 3. Re: Get strings lenght from txt file and put string num and its lenght in log
              EJP
              Is it a joke then? You kow how to get a line from a file into a string; you know how to get the length of a string; and you know how to write something to a file, but you can't write the length of a line read from a file to another file?

              Surely you jest.
              • 4. Re: Get strings lenght from txt file and put string num and its lenght in log
                902483
                it is not a joke. i don't know how to connect this parts of code to receive a result i need.

                is the direction of my thoughts correct?
                • 5. Re: Get strings lenght from txt file and put string num and its lenght in log
                  gimbal2
                  There is only one solution to your 'problem': learn programming. That is going to take some time and effort, until then you are not going to be productive in any way. Perhaps you want some book suggestions?
                  • 6. Re: Get strings lenght from txt file and put string num and its lenght in log
                    902483
                    Now i am reading Thinking in Java. But if you recomend me anything else I will be grateful. In any case, thanks.
                    • 7. Re: Get strings lenght from txt file and put string num and its lenght in log
                      902483
                      Ready!!! I wrote this program!!! But I have some problem: when I run it with input file which is more than 1,5Gb in size and 100000 lines in it then Eclipse console returns the message about mistake (see below). I increased VM arguments up to -Xms128m -Xmx1600m (in eclipse run configuration), but it didn’t help. Could anybody help me to decide this problem? Thank you.

                      Mistake message:

                      Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
                           at java.util.Arrays.copyOfRange(Unknown Source)
                           at java.lang.String.<init>(Unknown Source)
                           at java.lang.StringBuffer.toString(Unknown Source)
                           at java.io.BufferedReader.readLine(Unknown Source)
                           at java.io.BufferedReader.readLine(Unknown Source)
                           at org.apache.commons.io.IOUtils.readLines(IOUtils.java:771)
                           at org.apache.commons.io.IOUtils.readLines(IOUtils.java:723)
                           at org.apache.commons.io.IOUtils.readLines(IOUtils.java:745)
                           at org.apache.commons.io.FileUtils.readLines(FileUtils.java:1512)
                           at org.apache.commons.io.FileUtils.readLines(FileUtils.java:1528)
                           at org.apache.commons.io.ReadFileToListSample.main(ReadFileToListSample.java:43)

                      program 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<String> 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();
                      }
                      }
                      }
                      • 8. Re: Get strings lenght from txt file and put string num and its lenght in log
                        EJP
                        Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
                        So you are using too much memory.
                        // Using FileUtils.readLines()
                        // we can read file content line by line and return
                        // the result as a List of string.
                        But why? You already had working code that read the file line by line. You'll never run out of memory that way. Reading the entire file into a data structure is bound to blow up sooner or later.
                        // Iterate the result to print each line of the file.

                        fr = new FileReader(file);
                        lnr = new LineNumberReader(fr);
                        From this point on your code makes no sense whatsoever. Not only are you reading the entire file into memory, you are reading it twice. You can get rid of the FileUtils thing altogether. Just read a line at a time and get its length. You already had all the necessary code. All you needed to do was put it together. It could hardly be simpler:
                        line = lnr.readLine();
                        reads a line.
                        int length = line.length();
                        gives you the length. All you need to do is surround that with whatever you need, including checking for EOS when you've read a line. That's it.
                        • 9. Re: Get strings lenght from txt file and put string num and its lenght in log
                          902483
                          I mediate over it but without a result. =(
                          • 10. Re: Get strings lenght from txt file and put string num and its lenght in log
                            800268
                            Well, programming isn't a spiritual activity so meditating (assuming you meant that instead of http://www.merriam-webster.com/dictionary/mediate ) won't be of much use.

                            Most of programming can be reduced to:
                            1) Specify the problem (be specific what the problem is or what you don't understand)
                            2) Solve the problem or if you can't break the problem down into smaller steps (and goto 1 for each step).
                            3) Profit

                            So EJP did step 1 (reading the entire file at once causes a OutOfMemory exception) and step 2 (reading the file line-by-line so you need only to have one line in memory).
                            Since the you still have a problem with that solution, you need to break it down in smaller steps and find the solution to those steps. Be specific.

                            For example, my breakdown would be:
                            0) How do I open a file? (you should know this by now)
                            1) How do I read a line from a file (you should know this by now)
                            2) How do I determine the length of a line? (you should know this by now)
                            3) How do I do this for each line in a file? You seem to be struggling with this so I suggest you read:

                            http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
                            http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

                            4) How do I detect the end of a file? I suggest you read the javadoc of the class you will be using more closely:
                            http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

                            5) How do I close a file? If you're using Java 7 use a try-with-resources otherwise close the reader in a finally block.
                            See http://docs.oracle.com/javase/tutorial/essential/io/file.html for a more in depth (but also more complex) tutorial
                            • 11. Re: Get strings lenght from txt file and put string num and its lenght in log
                              EJP
                              I mediate over it but without a result. =(
                              Meditate. No need for that. All the code you need is already contains in your first post. Just join it up! and eliminate the bits you don't need.
                              • 12. Re: Get strings lenght from txt file and put string num and its lenght in log
                                902483
                                oh!
                                i suggested that the right way is to add code "int line_num = line.getLineNumber();" into "for" body

                                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 line_num = line.getLineNumber();

                                ........


                                but it is underlined wuth red. it means mistake in eclipse. =(

                                continue to meditate over it.
                                • 13. Re: Get strings lenght from txt file and put string num and its lenght in log
                                  800268
                                  May12 wrote:
                                  but it is underlined wuth red. it means mistake in eclipse. =(
                                  continue to meditate over it.
                                  No, don't meditate. Read and understand the compiler error (in eclipse, Window -> Show View -> Problems). If you don't, it might have been useful to post it here don't you think?
                                  • 14. Re: Get strings lenght from txt file and put string num and its lenght in log
                                    902483
                                    Problems:

                                    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
                                         The method getLineNumber() is undefined for the type String

                                         at org.apache.commons.io.ReadFileToListSample.main(ReadFileToListSample.java:53)