| | |
Exporting multiple Strings to a text file?
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
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:
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:
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)
try { Writer outputStream = new BufferedWriter(new FileWriter(outputLocation + "\\" + outputFile)); //print text to file outputStream.write(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11); }catch (Exception e) { JOptionPane.showMessageDialog ( null, "Error in writing data!!\n\r" + e, "File Save Error", JOptionPane.ERROR_MESSAGE); }
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)
public static String cloudOut1, cloudOut2, cloudOut3, cloudOut4, cloudOut5, cloudOut6, cloudOut7, cloudOut8, cloudOut9, cloudOut10, cloudOut11; public static String newLine = System.getProperty("line.separator"); public static File outputFile, outputLocation; try{ outputLocation = saveLocation.getCurrentDirectory(); outputFile = saveLocation.getSelectedFile(); }catch (Exception e) { JOptionPane.showMessageDialog ( null, "Error in writing data!!\n\r"+e, "File Save Error", JOptionPane.ERROR_MESSAGE); } System.out.println(outputLocation); try{ cloudOut1 = "In order to " + promptA.A + ", I must " + promptB.B + "." + newLine; cloudOut2 = "In order to " + promptB.B + ", I must " + promptD.D + "." + newLine + newLine; cloudOut3 = "On the other hand, In order to " + promptA.A + ", I must " + promptC.C + "." + newLine; cloudOut4 = "In order to " + promptC.C + ", I must " + promptDPrime.DPrime + "." + newLine; cloudOut5 = "I can't both " + promptD.D + " and " + promptDPrime.DPrime + "." + newLine; cloudOut6 = newLine + "In order to " + promptB.B + ", I must " + promptD.D + " because:" + newLine + promptBDAssump.BDAssump + newLine ; cloudOut7 = "Injection: " + promptBDInjection.BDInjection; cloudOut8 = newLine + "In order to " + promptC.C + ", I must " + promptDPrime.DPrime + " because:" + newLine + promptCDPrimeAssump.CDPrimeAssump + newLine ; cloudOut9 = "Injection: " + promptCDPrimeInjection.CDPrimeInjection ; cloudOut10 = newLine + "I can't both " + promptD.D + " and " + promptDPrime.DPrime + " because:" + newLine + promptDDPrimeAssump.DDPrimeAssump + newLine ; cloudOut11 = "Injection: " + promptDDPrimeInjection.DDPrimeInjection; }catch (Exception e) { JOptionPane.showMessageDialog ( null, "Error in writing data!!\n\r"+e, "File Save Error", JOptionPane.ERROR_MESSAGE); } System.out.println(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11); try { Writer outputStream = new BufferedWriter(new FileWriter(outputLocation + "\\" + outputFile)); //print text to file outputStream.write(cloudOut1 + cloudOut2 + cloudOut3 + cloudOut4 + cloudOut5 + cloudOut6 + cloudOut7 + cloudOut8 + cloudOut9 + cloudOut10 + cloudOut11); }catch (Exception e) { JOptionPane.showMessageDialog ( null, "Error in writing data!!\n\r" + e, "File Save Error", JOptionPane.ERROR_MESSAGE); }
Hello, amgupt01
You can use this:
You can use this:
java Syntax (Toggle Plain Text)
PrintWriter writer =new PrintWriter("yourfile"); 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
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
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Hello, amgupt01
You can use this:
java Syntax (Toggle Plain Text)
PrintWriter writer =new PrintWriter("yourfile"); writer.println("Line to print");
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...
•
•
Join Date: Sep 2008
Posts: 2,228
Reputation:
Solved Threads: 275
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"));
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.
Maybe you should try to do this:
P.S. And in conclusion, don't forget to close your writer after working with it
java Syntax (Toggle Plain Text)
writer.flush();
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
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
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?!
Sacrifice is a painful, pure and beautiful thing.
Dammit, Jones, What the Hell Are Knoll Pointers?!
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: To get ip in j2me
- Next Thread: GeoSolids
Views: 1296 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener add android applet application arguments array arraylist arrays binary c++ chat class classes client code component constructor convert data database db design desktop detection eclipse error event exception file forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image inheritance input integer interface j2me java javafx jframe jpanel jtextarea jtextfield key lazy linked linked-list list loop looping method methods mobile netbeans newbie node number object objects oracle output page pattern pixel problem programming read recursion remove return robot scanner search server service set size sms sql string swing system text text-file threads timer transfer translate tree user web







