Results 1 to 3 of 3
- 08-14-2011, 07:11 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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); } } }
Thanks in advance.
-
What line throws the NPE? Can you show us not just the line number, but the actual line of code itself?
- 08-14-2011, 07:28 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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); } } }
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
Problem writing multiple strings to a text file
By Yogesh_P in forum New To JavaReplies: 4Last Post: 03-30-2011, 12:58 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
writing to text file problem
By blumdiggity in forum NetworkingReplies: 1Last Post: 02-26-2010, 02:43 PM -
writing text to file
By notwist in forum New To JavaReplies: 3Last Post: 04-25-2008, 04:20 AM
Bookmarks