Nothing appears in file using following code. If I change the UserString to "Hello" for example it works and prints to the file. I've checked the value of UserString within try and it prints the correct value but i cant figure out why the printWriter won't use the value.
public void writeToAFile()
{
FileOutputStream outputStream = null;
PrintWriter printWriter = null;
System.out.println("Please enter the name of the file:");
Scanner UserFileNameInput = new Scanner(System.in);
FileName = UserFileNameInput.nextLine();
if(FileName != "")
{
do {
System.out.println("Please enter a string:");
Scanner UserStringInput = new Scanner(System.in);
UserString = UserStringInput.nextLine();
try
{
outputStream = new FileOutputStream(FileName+".txt");
printWriter = new PrintWriter(outputStream);
printWriter.println(UserString);
}
catch(IOException e)
{
System.out.println("Error in file write: " + e);
}
}while(UserString.length() != 0);
printWriter.close();
System.exit(0);
}
else
{
System.out.println("Please enter a valid input");
writeToAFile();
}
}
public static void main(String[] args)
{
Files writeToAFileObject = new Files();
writeToAFileObject.writeToAFile();
}