I'm having a problem with writing a text file using Java swing. Below is my code, if I try to open the file the text isn't formatted like its supposed to, going into a new line after each input text. I've tried using /n, System.lineSeparator() and %n, but nothing seems to work. Unless I copy the content into another file it looks wrong. Instead of going into a new line it gives me this:
Name: Bob Test Number: 12345678 Email: BobTest@email.com
Address: Teststreet 123 TestCity Testcountry
Gender: Male Occupation: Student Salary: 500 Manager: Yes
try {
Object fileName = textField.getText();
File user = new File("C:\\Users\\Nicol\\Desktop\\"+fileName+".txt");
if(!user.exists()) {
user.createNewFile();
}
PrintWriter pw = new PrintWriter(user);
pw.print("Name: ");
textField.write(pw);
pw.print("\nNumber: ");
textField_1.write(pw);
pw.print("\nEmail: ");
textField_3.write(pw);
pw.print("\n\nAddress: \n");
textArea.write(pw);
if(rdbtnMale.isSelected()) {
pw.print("\n\nGender: Male");
}else if(rdbtnFemale.isSelected()) {
pw.print("\n\nGender: Female");
}else {
pw.print("\n\nGender: none selected");
}
Object professionSelect = comboBox.getSelectedItem();
pw.print("\nOccupation: " + professionSelect);
pw.print("\nSalary: ");
textField_4.write(pw);
if(chckbxIsManager.isSelected()) {
pw.print("\nManager: Yes");
}
else {
pw.print("\nManager: No");
}
JOptionPane.showMessageDialog(null, "The file has been created on the desktop.");
pw.close();
}