I am a novice. I am trying to take the user-input text from the JOptionPane, and store it into a text file. Thereafter I would like to read the text and do what-not with it.
May I please have help on storing the inputted text? Thanks. Here's my code:
import javax.swing.JOptionPane;
import java.io.*;
public class RunProgram {
public static void introView() {
//The introduction
JOptionPane.showMessageDialog(null, "Welcome." +
" To begin, please click the below button to input some information " +
"about yourself.");
}
public static void personInput() {
try{
File userInfo = new File("C:\\Users\\WG Chasi\\workspace\\" +
"Useful Java\\products\\UserInfo.txt");
userInfo.getParentFile().mkdirs();
FileWriter input = new FileWriter(userInfo);
JOptionPane userInput = new JOptionPane();
userInput.showInputDialog("Enter details");/*I want to store the text from the InputDialog into the text file*/
//Write text from the JOptionPane into UserInfo.txt
}catch(Exception e){
JOptionPane.showMessageDialog(null, "An ERROR has occured.");
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
introView();
personInput();
}
});
}
}
You have any number of potential options, depending on your needs...
You could...
Write the contents to a
Properties
file...As you can see, you have to physically load and save the contents yourself. This does mean, however, you get to control the location of the file...
You could...
Make use of the
Preferences
API...Then you would simply use something like...
to retrieve the values.
The benefit of this is the storage process is taking care for you, but you lose control of where the data is stored.
You could...
Try this
I am basically attempting to get the text from the input dialog and write it to a file of your choice. The file will be written as a text file using the appending string ".txt" which sets the mime type so will always be text.
Let me know how it goes.