3 Replies Latest reply: Oct 25, 2007 3:08 PM by 807603 RSS

    To write a text file in a web page

    807603
      I need to write last 10 lines of a text file to a web page. I could write the entire file using following code...but how to write last x lines?????
      import java.io.*;
      public class ReadWriteTextFile {
      /**
      * Fetch the entire contents of a text file, and return it in a String.
      * This style of implementation does not throw Exceptions to the caller.
      *
      * @param aFile is a file which already exists and can be read.
      */
      static public String getContents(File aFile) {
      //...checks on aFile are elided
      StringBuffer contents = new StringBuffer();

      //declared here only to make visible to finally clause
      BufferedReader input = null;
      try {
      //use buffering, reading one line at a time
      //FileReader always assumes default encoding is OK!
      input = new BufferedReader( new FileReader(aFile) );
      String line = null; //not declared within while loop
      /*
      * readLine is a bit quirky :
      * it returns the content of a line MINUS the newline.
      * it returns null only for the END of the stream.
      * it returns an empty String if two newlines appear in a row.
      */
      while (( line = input.readLine()) != null){
      contents.append(line);
      contents.append(System.getProperty("line.separator"));
      }
      }
      catch (FileNotFoundException ex) {
      ex.printStackTrace();
      }
      catch (IOException ex){
      ex.printStackTrace();
      }
      finally {
      try {
      if (input!= null) {
      //flush and close both "input" and its underlying FileReader
      input.close();
      }
      }
      catch (IOException ex) {
      ex.printStackTrace();
      }
      }
      return contents.toString();
      }

      Edited by: Dhanish.Davis on Oct 25, 2007 6:21 AM
        • 1. Re: To write a text file in a web page
          807603
          First of all, from now on you should enter your code into code tags. It makes it much more readable and people will be more inclined to help you.

          What you are trying to do is easy enough you just have to change your approach a little bit. Instead of appending the String variable line into your StringBuffer, just add it to a String array.

          Then in a seperate for loop you do something like this...
          //String[] stringArray
          
          for (int i = (stringArray.length - 10); i < stringArray.length; i++) {
              contents.append(stringArray);
          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
          • 2. Re: To write a text file in a web page
            807603
            Rather than using a String array, it would probably be better and more reusable to use an ArrayList. If you want to use the code for more than one text file or a text file whose content changes, an ArrayList is more appropriate. Using a String array would require you know the number of lines in the text file prior to reading it, and you would need to modify your initialization code each time the text file changes.
            • 3. Re: To write a text file in a web page
              807603
              Good catch... I didn't think of that right then.