943,844 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1649
  • Java RSS
Dec 7th, 2008
0

Exporting multiple Strings to a text file?

Expand Post »
Hi guys,

I am a beginner to Java (I'm only a freshman in high school) and I'm working on a project which involves the need for me to be able to store 11 different strings as a text file. I have it storing the strings with multiple lines so I set a string newLine = System.getProperty("line.separator");

The part that is screwing up is this:
Java Syntax (Toggle Plain Text)
  1. try {
  2. Writer outputStream = new BufferedWriter(new FileWriter(outputLocation + "\\" + outputFile));
  3. //print text to file
  4. outputStream.write(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11);
  5. }catch (Exception e) {
  6. JOptionPane.showMessageDialog (
  7. null,
  8. "Error in writing data!!\n\r" + e,
  9. "File Save Error",
  10. JOptionPane.ERROR_MESSAGE);
  11. }

Everywhere I've looked, I find sites saying that I should do it the way that's written above. I think that if I find out how to write a text file one line at a time, I might be able to make it work because I think that the newLine's are messing it up...

Thanks for your help,

~Ankush

P.S: If you need the code for everything that I'm using:
Java Syntax (Toggle Plain Text)
  1. public static String cloudOut1, cloudOut2, cloudOut3, cloudOut4, cloudOut5, cloudOut6, cloudOut7, cloudOut8, cloudOut9, cloudOut10, cloudOut11;
  2. public static String newLine = System.getProperty("line.separator");
  3. public static File outputFile, outputLocation;
  4. try{
  5. outputLocation = saveLocation.getCurrentDirectory();
  6. outputFile = saveLocation.getSelectedFile();
  7. }catch (Exception e) {
  8. JOptionPane.showMessageDialog (
  9. null,
  10. "Error in writing data!!\n\r"+e,
  11. "File Save Error",
  12. JOptionPane.ERROR_MESSAGE);
  13. }
  14.  
  15. System.out.println(outputLocation);
  16.  
  17.  
  18. try{
  19. cloudOut1 = "In order to " + promptA.A + ", I must " + promptB.B + "." + newLine;
  20. cloudOut2 = "In order to " + promptB.B + ", I must " + promptD.D + "." + newLine + newLine;
  21. cloudOut3 = "On the other hand, In order to " + promptA.A + ", I must " + promptC.C + "." + newLine;
  22. cloudOut4 = "In order to " + promptC.C + ", I must " + promptDPrime.DPrime + "." + newLine;
  23. cloudOut5 = "I can't both " + promptD.D + " and " + promptDPrime.DPrime + "." + newLine;
  24.  
  25. cloudOut6 = newLine + "In order to " + promptB.B + ", I must " + promptD.D + " because:" + newLine + promptBDAssump.BDAssump + newLine ;
  26. cloudOut7 = "Injection: " + promptBDInjection.BDInjection;
  27.  
  28. cloudOut8 = newLine + "In order to " + promptC.C + ", I must " + promptDPrime.DPrime + " because:" + newLine + promptCDPrimeAssump.CDPrimeAssump + newLine ;
  29. cloudOut9 = "Injection: " + promptCDPrimeInjection.CDPrimeInjection ;
  30.  
  31. cloudOut10 = newLine + "I can't both " + promptD.D + " and " + promptDPrime.DPrime + " because:" + newLine + promptDDPrimeAssump.DDPrimeAssump + newLine ;
  32. cloudOut11 = "Injection: " + promptDDPrimeInjection.DDPrimeInjection;
  33. }catch (Exception e) {
  34. JOptionPane.showMessageDialog (
  35. null,
  36. "Error in writing data!!\n\r"+e,
  37. "File Save Error",
  38. JOptionPane.ERROR_MESSAGE);
  39. }
  40.  
  41. System.out.println(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11);
  42.  
  43. try {
  44. Writer outputStream = new BufferedWriter(new FileWriter(outputLocation + "\\" + outputFile));
  45. //print text to file
  46. outputStream.write(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11);
  47. }catch (Exception e) {
  48. JOptionPane.showMessageDialog (
  49. null,
  50. "Error in writing data!!\n\r" + e,
  51. "File Save Error",
  52. JOptionPane.ERROR_MESSAGE);
  53. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amgupt01 is offline Offline
6 posts
since Nov 2007
Dec 7th, 2008
0

Re: Exporting multiple Strings to a text file?

Hello, amgupt01
You can use this:
java Syntax (Toggle Plain Text)
  1. PrintWriter writer =new PrintWriter("yourfile");
  2. writer.println("Line to print");
Last edited by Antenka; Dec 7th, 2008 at 1:28 pm.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Dec 7th, 2008
0

Re: Exporting multiple Strings to a text file?

Click to Expand / Collapse  Quote originally posted by Antenka ...
Hello, amgupt01
You can use this:
java Syntax (Toggle Plain Text)
  1. PrintWriter writer =new PrintWriter("yourfile");
  2. writer.println("Line to print");
Thanks for your response!

I tried this and all that it does is make a blank file... It has the right filename (I just made it print.txt), but the file is completely blank... Even when I tried writing something like "testing" in quotation marks instead of using the strings that I need to, the output just is a blank file with the name print.txt...

I can't think of what the problem is...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amgupt01 is offline Offline
6 posts
since Nov 2007
Dec 7th, 2008
0

Re: Exporting multiple Strings to a text file?

http://java.sun.com/j2se/1.4.2/docs/...intWriter.html

Look at the API. Do you see a constructor for PrintWriter that takes a String as an argument? (Change your code so that your PrintWriter takes the correct argument)

You might want to look at this link, since from the API, it isn't immediately obvious which type of Object to create: http://java.sun.com/docs/books/tutor...tTogether.html

Or, here's a quick example,

PrintWriter writer = new PrintWriter(new FileWriter("yourFile.txt"));
Last edited by BestJewSinceJC; Dec 7th, 2008 at 2:10 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Dec 7th, 2008
0

Re: Exporting multiple Strings to a text file?

ooops
sorry for misleading
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Dec 7th, 2008
1

Re: Exporting multiple Strings to a text file?

Its cool, Antenka. Everybody makes mistakes. What I usually do before responding is quickly check the API to make sure my response coincides with what I see there.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Dec 7th, 2008
0

Re: Exporting multiple Strings to a text file?

hmm... I tried the way BestJew showed and it still gives me a blank file...

I don't understand what's wrong with it though... I had, in fact already tried BestJew's way before I posted this and it didn't work so I just tried a bunch of other stuff...

Thanks for your help though...

~Ankush
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amgupt01 is offline Offline
6 posts
since Nov 2007
Dec 8th, 2008
0

Re: Exporting multiple Strings to a text file?

Maybe you should try to do this:
java Syntax (Toggle Plain Text)
  1. writer.flush();
P.S. And in conclusion, don't forget to close your writer after working with it
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Dec 8th, 2008
0

Re: Exporting multiple Strings to a text file?

In the code posted I don't see the stream/writer being closed. Variable names like cloudOut1 through cloudOut11 suggest there is something seriously wrong with your design. Try writing out a small and compilable test case which illustrates the problem at hand at post it here.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 17th, 2008
0

Re: Exporting multiple Strings to a text file?

http://java.sun.com/j2se/1.4.2/docs/...intWriter.html

Look at the API. Do you see a constructor for PrintWriter that takes a String as an argument? (Change your code so that your PrintWriter takes the correct argument)
Since the release of 1.5 the PrintWriter class does have a constructor that can take a String representation of a filename .
http://java.sun.com/j2se/1.5.0/docs/...intWriter.html

Antenka hit the nail on the head with the invocation of the flush() method.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gprest is offline Offline
1 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: To get ip in j2me
Next Thread in Java Forum Timeline: GeoSolids





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC