1 2 Previous Next 25 Replies Latest reply: Oct 14, 2008 8:53 AM by 795402 RSS

    read text file line by line and print

    807600
      I want my java program to read a comma seperated text file and put only the valid records (*valid only if 3rd column is equal to "html" or "swf"*) in another text file with count of each slide type. Can someone put me in the right path?

      i tried using readLine() methed and split() method to split the comma seperated text file but, i dont know how to move from first line to next line and till end of the file. should i use two dimentional array or seek() method?

      let me know whichever is easier? it will be very helpful for me if you can give a pseudocode for this.
        • 1. Re: read text file line by line and print
          807600
          use a Scanner object
          it has many useful methods: hasNextLine() and nextLine() will be most useful to you.

          Pseudo code:
          while scanner has next line
              get the next line
              split it using comma as a delimeter
              validate the record ( check if 3rd item equals "html" or ...)
              if valid
                write line to other text file
          • 2. Re: read text file line by line and print
            807600
            If you're using BufferedReader, then readLine() will always just read the next line of text. You don't have to advance the line separately. readLine() returns null at end-of-file. So a common idiom is:
            BufferedReader in = ..
            String line;
            while((line = in.readLine()) != null) {
                // do something with line
            }
            However, you should keep in mind that CSV files can have embedded commas in fields, so a simple split (or even a regexp query like you get out of Scanner) may not be sufficient. Probably the easiest thing is to download and use a library for parsing CSV files. I believe that Apache Jakarta has one.
            • 3. Re: read text file line by line and print
              807600
              Can i use bufferedreader if i use scanner? or it is simply a replacement for bufferedreader?
              • 4. Re: read text file line by line and print
                807600
                i have converted from CSV to .txt file. It has comma within the field. i dont know how to manage this. i wanted to check with the 3rd column alone and the 6th column also has comma within the field. so i have no choice other than ignoring that column

                how do i check whether end of file is encountered or not in my loop?
                • 5. Re: read text file line by line and print
                  masijade
                  Google for a "Java CSV API".
                  • 6. Re: read text file line by line and print
                    807600
                    madhukar_has_a_doubt wrote:
                    Can i use bufferedreader if i use scanner? or it is simply a replacement for bufferedreader?
                    You can use a BufferedReader to read lines and a Scanner to read stuff within a line, if you like, but there's really no point. Scanner and BufferedReader have overlapping capabilities and normally you'd use one or the other. Scanner is more like a swiss army knife for reading user input; BufferedReader handles a particular IO issue.

                    You're still better off using an existing CSV parsing library.

                    Here's one:
                    http://commons.apache.org/sandbox/csv/
                    • 7. Re: read text file line by line and print
                      807600
                      madhukar_has_a_doubt wrote:
                      i have converted from CSV to .txt file. It has comma within the field. i dont know how to manage this. i wanted to check with the 3rd column alone and the 6th column also has comma within the field. so i have no choice other than ignoring that column
                      You're best off managing it by using an existing library to parse CSV files.

                      <sigh> but if you refuse to, you can either:

                      1) write your own parser, or

                      2) perhaps you can write a regular expression smart enough to handle CSV fields with embedded commas. This is a little bit easier if you can at least guarantee that there are no embedded quotes within a field. Do you know how to write regular expressions?
                      how do i check whether end of file is encountered or not in my loop?
                      The whole Scanner paradigm seems to be to use methods with names like "hasNext" to tell you if you have any more input.
                      • 8. Re: read text file line by line and print
                        807600
                        Someone please correct my code so that my program reads the comma seperated text file line by line and

                        checks for a specific data(swf or image) in a 6th column and (Questions) in the 2nd column.

                        package readingFromTextFile;
                        
                        import java.io.BufferedReader;
                        import java.io.FileReader;
                        
                        public class MainClass 
                        {
                             public static void main(String args[])
                             {
                                  try 
                                  {
                                       FileReader fr=null;
                                       BufferedReader br = new BufferedReader(new FileReader("test.txt"));
                        
                                       String s;
                                       while((s = br.readLine()) != null)
                                       {
                                            String[] tkn =s.split(",",-1);
                                            for(int i=0; i<tkn.length;i++)
                                            {
                                                 System.out.println(tkn);
                                                 /*if(tkn[6]=="application/x-shockwave-flash")
                                                 {
                                                      System.out.println("SWF file found ");
                                                 }
                                                 if(tkn[6]=="image/jpeg")
                                                 {
                                                      System.out.println("Image file found ");
                                                 }
                                                 if(tkn[2]=="Questions")
                                                 {
                                                      System.out.println("Questions found");
                                                 }
                                                 else
                                                 {
                                                      System.out.println("Other type of slides found");
                                                 }*/
                                                 
                                            }
                                       }
                                       fr.close();
                                  }
                                  catch(Exception e)
                                  {
                                       System.out.println("Exception: " + e);
                                  }
                             }
                        }
                        the ablove code prints the output in the following format
                        //Output:

                        AAAAA
                        1111111
                        2222222
                        http:test.html
                        application/x-url

                        222222
                        XXXXXXXXXX
                        Image
                        599204
                        483580
                        483579/media.jpg
                        image/jpeg

                        795
                        My file
                        Code
                        8
                        9
                        sample.swf
                        application/x-shockwave-flash

                        1296
                        stamps
                        New Polling
                        4863
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                        • 9. Re: read text file line by line and print
                          807600
                          Here's what you did wrong: you didn't use a CSV-parsing library.
                          • 10. Re: read text file line by line and print
                            3004
                            madhukar_has_a_doubt wrote:
                            Someone please correct my code
                            No.

                            As I told you in the other thread where you asked this, that's YOUR responsibility. All you should expect here is general principles and ideas of what approach to take.

                            Don't keep asking the same question in multiple threads. It's rude, and has already made me decide never to help you again.
                            • 11. Re: read text file line by line and print
                              3004
                              paulcw wrote:
                              Here's what you did wrong: you didn't use a CSV-parsing library.
                              I thought what he did wrong was being a rude, stupid, crossposting assmonkey.
                              • 12. Re: read text file line by line and print
                                796447
                                warnerja has a doubt that madhukar will ever have what it takes to be a software developer.
                                • 13. Re: read text file line by line and print
                                  807600
                                  Look: it's almost certain that this is a homework assignment. If so you're basically asking us to do your homework for you.

                                  If it's not a homework assignment, then you've repeatedly refused to do the logical, easy thing, despite being repeatedly advised to do so. In this case, you're being difficult. We're not going to help you do things the wrong way.

                                  Try doing your own homework. If you have an actual problem (note: "I don't get it" is not a problem, it's passing the buck), then feel free to ask, but don't expect anyone to put more effort into this than you are.
                                  • 14. Re: read text file line by line and print
                                    807601
                                    i need a code for reading CSV file and remove the comma's in that file and write that data into another file with with seperated by semicolon ,instead of comma's and that file extension also .csv.

                                    example :we have twofiles "Infile.csv" and "Outfiel.csv" ,here infile have a data with cama seperated values,and i need a same data with semicolon seperated value

                                    Infile.csv:-this fiel have a data like
                                    mohana,ravali,software,engineer,,0

                                    output should be Outfile.csv:-
                                    mohana;ravali;software;engineer ; ; 0

                                    please send that code to **mohanas20@gmail.com**
                                    1 2 Previous Next