8 Replies Latest reply: Aug 1, 2007 12:28 AM by 800705 RSS

    Reading Strings

    800705
      File f = new File("C:\\\\OracleUpload1.txt");
      FileOutputStream os =new FileOutputStream(f);
      PrintWriter pw = new PrintWriter(os);
      File f1 = new File("C:\\\\TestParseOracle.txt");
      FileReader fr = new FileReader(f1);
      int counter = 0;
      int temp = 0;
      char ch[] = new char[1];
      while(counter != 2){
      temp = fr.read(ch);
      pw.print(ch);
                            
      if(temp == 44){
      counter++;
      pw.println();
      pw.flush();
      }
                           
      }
      The above is my code fragment. I am using the above to print out strings from a CSV format text file into another text file so that only the first two strings of each line of the first file are written onto the second file.

      I am using ASCII code 44 to check for a comma.

      What is wrong in the program?
        • 1. Re: Reading Strings
          800322
          What is wrong in the program?
          You tell us. We'll tell you how to fix it.

          By the way, what's wrong with just reading a line, splitting it on "," and printing the first two items of the resulting array?
          • 2. Re: Reading Strings
            807605
            <I am using ASCII code 44 to check for a comma.

            Why? Just use charAt() and substring
            • 3. Re: Reading Strings
              807605
              The above is my code fragment. I am using the above
              to print out strings from a CSV format text file into
              another text file so that only the first two strings
              of each line of the first file are written onto the
              second file.

              I am using ASCII code 44 to check for a comma.

              What is wrong in the program?
              1) I don't see any concept of 'lines' so at best you will only process the first line.
              2) Your approach will fail if the fields contain embedded commas.
              3) Your approach will alway print a trailing comma.

              Look at BufferedInputStream to see how to read lines.
              Look at String.indexof() to see how to find commas.
              • 4. Re: Reading Strings
                807605
                its best if you forget about the existence of the char datatype, it will only make your life suck
                • 5. Re: Reading Strings
                  800705
                  if the lines in the input file are too long then would not using BufferedReader give erroneous results?
                  • 6. Re: Reading Strings
                    800705
                    I would like to know how to use split to separate tokens when either ',' or '\n' is the separator.

                    Message was edited by:
                    subhashmedhi
                    • 7. Re: Reading Strings
                      800705
                      File f = new File("C:\\\\OracleUpload1.txt");
                                FileOutputStream os =new FileOutputStream(f);
                                      PrintWriter pw = new PrintWriter(os);
                                      File f1 = new File("C:\\\\TestParseOracle.txt");
                                      FileReader fr = new FileReader(f1);
                                      int counter = 0;
                                      int temp = 0;
                                      char ch[] = new char[1];
                                      while( counter != 2){
                                            temp = fr.read(ch);
                                            pw.print(ch);
                                            if(ch[0] == ','){
                                               pw.println();
                                               pw.flush();
                                                counter++;
                                            }
                                               
                                      }
                      if i want to print the first two tokens from a CSV format text file would the above code work or not?
                      • 8. Re: Reading Strings
                        800705
                        If i use BufferedReader i am facing the following problem:

                        one of the strings in the CSV file is having a '/' in the middle. BufferedReader assumes this to be the end of the line and hence retrievs the line only upto that. the remianing portion of the line becomes another line.

                        Suppose the line in the input text file is:
                        Oracle Code,Item Description for PO,Description for POannexure,null,Part Code/MUS Code,Basic Unit Price,Excise Duty,Education cess,CST (Form C),FOR Destination Unit Price


                        the BufferedReader assumes the line to be
                        Oracle Code,Item Description for PO,Description for POannexure,null,Part Code/ i.e. upto the slash("/") and the remaining portion is taken to be another line.

                        How to avoid this?