如何将JOptionPane中的文本存储到文本文件中 [英] How to store text from JOptionPane into text file

查看:166
本文介绍了如何将JOptionPane中的文本存储到文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。我试图从JOptionPane获取用户输入文本,并将其存储到文本文件中。此后,我想阅读文本,不做任何事情。

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...

将内容写入属性文件...

Write the contents to a Properties file...

private Properties properties = new Properties();

//...
String name = JOptionPane.showInputDialog("What is your name?");
properties.set("user.name", name);

//...
protected void savePropeties() throws IOException {
    try (OutputStream os = new FileOutputStream(new File("User.properties"))) {
        properties.store(os, "User details");
    }
}

protected void loadPropeties() throws IOException {
    try (InputStream is = new FileInputStream(new File("User.properties"))) {
        // Note, this will overwrite any previously existing
        // values...
        properties.load(is);
    }
}

如您所见,您必须实际加载和自己保存内容。但这确实意味着你可以控制文件的位置......

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...

使用偏好设置 API ...

Make use of the Preferences API...

String name = JOptionPane.showInputDialog("What is your name?");
Preferences preferences = Preferences.userNodeForPackage(RunProgram.class);
preferences.put("user.name", name);

然后您只需使用类似......

Then you would simply use something like...

Preferences preferences = Preferences.userNodeForPackage(RunProgram.class);
String name = preferences.get("user.name", null);

检索价值。

这样做的好处是存储过程正在照顾你,但你无法控制数据的存储位置。

The benefit of this is the storage process is taking care for you, but you lose control of where the data is stored.


  • 以您自己的格式自行将数据写入文件。这需要大量工作和开销,但您不仅可以获得控制文件位置的好处,还可以获得维护数据的格式。请参阅基本I / O

  • 以XML格式写入数据,这提供了一个级别分级控制(如果这很重要),但确实增加了管理的复杂性。

这篇关于如何将JOptionPane中的文本存储到文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆