Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default Concatenation file contents

    The readFile(...) method presented below reads a file and returns the string with the content. We have used StringBuffer to perform concatenation operations.

    Java Code:
    private static String readFile(String filename) throws IOException {
       String lineSep = System.getProperty("line.separator");
       BufferedReader br = new BufferedReader(new FileReader(filename));
       String nextLine = "";
       StringBuffer sb = new StringBuffer();
       while ((nextLine = br.readLine()) != null) {
         sb.append(nextLine);
         sb.append(lineSep);
       }
       return sb.toString();
    }

  2. #2
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    good example !!!
    i appreciate the above one.
    Please look also for the differences in String,StringBuffer and StringBuilder classes.
    Just trying to make things a bit flexible for you.

Similar Threads

  1. Viewing contents of zip file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 05:16 PM
  2. Adding file contents to Choice component
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:06 AM
  3. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:00 AM
  4. Viewing contents of JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 03:12 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •