problem with read line by line using BufferReader class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdsutaia
    New Member
    • Nov 2006
    • 15

    problem with read line by line using BufferReader class

    hi!
    I am trying to read file line by line using BufferReader class.Following I have written to read but some how for few of the file it is not reading the whole file. Can any help me to resolve this or suggest me the best apporch to read line by line from given file ?


    try {
    br = FileRead.readTO Line(path ,filename);
    while ((line =br.readLine()) != null){
    line = line.toUpperCas e().trim();
    ltrim(line);
    rtrim(line);
    System.out.prin tln(line);
    System.out.flus h();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTra ce();
    }




    public class FileRead {



    /* Pass filename and path as input
    * Return BufferedReader
    */
    public static BufferedReader readTOLine(Stri ng path, String filename) throws IOException{

    BufferedReader br= new BufferedReader (new FileReader (path + filename));

    //br.close();
    return br;
    }

    /*
    * Pass Reader as input
    * Return String
    * */
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by kdsutaia
    hi!
    I am trying to read file line by line using BufferReader class.Following I have written to read but some how for few of the file it is not reading the whole file. Can any help me to resolve this or suggest me the best apporch to read line by line from given file ?
    ...
    Are there excpetion thrown? If so, which ones?
    Could you post an example of a file that is partially read? (if not too big)

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I normally read a file line by line like this:

      [code=java]
      public void readFile(String name) throws IOException {
      BufferedReader br= new BufferedReader( new FileReader(name ));
      String line;
      while ((line= br.readLine()) != null)
      processLine(lin e);
      br.close();
      }[/code]

      Use it the way you want.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        I normally read a file line by line like this:

        [code=java]
        public void readFile(String name) throws IOException {
        BufferedReader br= new BufferedReader( new FileReader(name ));
        String line;
        while ((line= br.readLine()) != null)
        processLine(lin e);
        br.close();
        }[/code]

        Use it the way you want.

        kind regards,

        Jos
        Notice how he wrapped his code in code tags ...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Notice how he wrapped his code in code tags ...
          I've always been a fine example to humanity ;-)

          kind regards,

          Jos

          Comment

          Working...