Exporting multiple Strings to a text file?

Reply

Join Date: Nov 2007
Posts: 6
Reputation: amgupt01 is an unknown quantity at this point 
Solved Threads: 0
amgupt01 amgupt01 is offline Offline
Newbie Poster

Exporting multiple Strings to a text file?

 
0
  #1
Dec 7th, 2008
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:
  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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Exporting multiple Strings to a text file?

 
0
  #2
Dec 7th, 2008
Hello, amgupt01
You can use this:
  1. PrintWriter writer =new PrintWriter("yourfile");
  2. writer.println("Line to print");
Last edited by Antenka; Dec 7th, 2008 at 12:28 pm.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: amgupt01 is an unknown quantity at this point 
Solved Threads: 0
amgupt01 amgupt01 is offline Offline
Newbie Poster

Re: Exporting multiple Strings to a text file?

 
0
  #3
Dec 7th, 2008
Originally Posted by Antenka View Post
Hello, amgupt01
You can use this:
  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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2,228
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 275
BestJewSinceJC BestJewSinceJC is offline Offline
Nearly a Posting Maven

Re: Exporting multiple Strings to a text file?

 
0
  #4
Dec 7th, 2008
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 1:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Exporting multiple Strings to a text file?

 
0
  #5
Dec 7th, 2008
ooops
sorry for misleading
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2,228
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 275
BestJewSinceJC BestJewSinceJC is offline Offline
Nearly a Posting Maven

Re: Exporting multiple Strings to a text file?

 
1
  #6
Dec 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: amgupt01 is an unknown quantity at this point 
Solved Threads: 0
amgupt01 amgupt01 is offline Offline
Newbie Poster

Re: Exporting multiple Strings to a text file?

 
0
  #7
Dec 7th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Exporting multiple Strings to a text file?

 
0
  #8
Dec 8th, 2008
Maybe you should try to do this:
  1. writer.flush();
P.S. And in conclusion, don't forget to close your writer after working with it
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,772
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Exporting multiple Strings to a text file?

 
0
  #9
Dec 8th, 2008
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.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: gprest is an unknown quantity at this point 
Solved Threads: 0
gprest gprest is offline Offline
Newbie Poster

Re: Exporting multiple Strings to a text file?

 
0
  #10
Dec 17th, 2008
Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 1296 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC