Results 1 to 4 of 4

Thread: Writing a file in neat format

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved Writing a file in neat format

    I am using the FileWriter() class to write a file. But i'm not getting what i want.
    Code:
    Bill JonesJohn SmithMike BurnsDaniel MorganPeter Hughes
    I dont want this, but i want this.
    Code:
    Bill Jones
    John Smith
    Mike Burns
    Daniel Morgan
    Peter Hughes
    How can i get this? Can you get this using the FileWriter class as it doesn't appear that the class can do a carriage return when writing a file?
    Last edited by x-ice; Feb 25th, 2007 at 07:09 PM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Writing a file in neat format

    write a "\n" after each name
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Writing a file in neat format

    Quote Originally Posted by ComputerJy
    write a "\n" after each name
    That didn't work, can you show me what you mean with example code?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Writing a file in neat format

    Code:
    import java.io.*;
    
    public class Sample {
      public static void main(String args[]) {
        String names[] = {
            "Bill Jones", "John Smith", "Mike Burns", "Daniel Morgan",
            "Peter Hughe"};
        FileWriter f = null;
        try {
          f = new FileWriter("C:\\a.txt");
          for (int i = 0; i < names.length; i++) {
            f.write(names[i] + " " + System.getProperty("line.separator"));
          }
          f.close();
        }
        catch (IOException ex) {
        }
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width