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);
}