java如何将JOptionPane中的文本存储到文本文件中
我是个新手。我试图从JOptionPane中获取用户输入的文本,并将其存储到文本文件中。此后,我想阅读课文,并用它做什么
能帮我保存输入的文本吗?谢谢 这是我的代码:
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();
}
});
}
}
# 1 楼答案
试试这个
我基本上是试图从输入对话框中获取文本,并将其写入您选择的文件中。该文件将使用附加字符串“.txt”作为文本文件写入,该字符串将mime类型设置为文本
告诉我进展如何
# 2 楼答案
根据你的需要,你有很多潜在的选择
你可以
将内容写入
Properties
文件正如您所见,您必须亲自加载并保存内容。这意味着,你可以控制文件的位置
你可以
利用
Preferences
API然后你可以简单地使用像
检索值
这样做的好处是,存储过程会照顾到您,但您会失去对数据存储位置的控制
你可以