Results 1 to 3 of 3
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Problem with writing out a text file

    Hello again folks

    Here's my issue. I've written a simple program that takes a files content and stores them in a HashMap so that it removes duplicates. So far so good. Now I want to write out the contents of the HashMap to a text file, with a CRLF after each entry has been written. Here is my code:

    Java Code:
    public void writeFile()
          {
             String pathname = OUFileChooser.getFilename();
             File aFile = new File(pathname);
             ObjectOutputStream outStream = null;
             try
                {
    			   FileWriter writer = new FileWriter(aFile);
                Iterator it = noDuplicates.iterator();
                   while (it.hasNext())
                   {
                      writer.append(it.next().toString());
    				      writer.append('\n');
    				      System.out.println(noDuplicates.toString());
                   }
                }
             
                catch (Exception anException)
                {
                   System.out.println("Error: " + anException);
                }
                finally
             {
              try
                 {
                    outStream.close();
                 }
              catch (Exception anException)
                 {
                    System.out.println("Error: " + anException);
                 }
              }
             }
    What I get though is an empty file and a nullPointerException. Any ideas where I'm going wrong? This isn't a homework question, just a pet project of mine.

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Hi Fubarable

    Sorry about this, quickly realised my mistake and fixed it. For other members here's my solution:

    Java Code:
     public void writeFile()
          {
             String pathname = OUFileChooser.getFilename();
             File aFile = new File(pathname);
             //ObjectOutputStream outStream = null;
             FileWriter writer = null;
             try
                {
    			   writer = new FileWriter(aFile);
                Iterator it = noDuplicates.iterator();
                   while (it.hasNext())
                   {
                      writer.append(it.next().toString());
    				      writer.append('\n');
    				      System.out.println(noDuplicates.toString());
                   }
                }
             
                catch (Exception anException)
                {
                   System.out.println("Error: " + anException);
                }
                finally
             {
              try
                 {
                    writer.close();
                 }
              catch (Exception anException)
                 {
                    System.out.println("Error: " + anException);
                 }
              }
             }
    Thanks folks

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 12:58 AM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  5. writing text to file
    By notwist in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 04:20 AM

Posting Permissions

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