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:

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:

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:

PrintWriter writer =new PrintWriter("yourfile");
writer.println("Line to print");

Hello, amgupt01
You can use this:

PrintWriter writer =new PrintWriter("yourfile");
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... :(

http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintWriter.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/tutorial/essential/exceptions/putItTogether.html

Or, here's a quick example,

PrintWriter writer = new PrintWriter(new FileWriter("yourFile.txt"));

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.

Comments
Thanks for correction and moral support :)

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

Maybe you should try to do this:

writer.flush();

P.S. And in conclusion, don't forget to close your writer after working with it :)

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.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintWriter.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/api/java/io/PrintWriter.html

Antenka hit the nail on the head with the invocation of the flush() method.

This article has been dead for over six months. Start a new discussion instead.