Java 如何将JOptionPane中的文本存储到文本文件中

Java 如何将JOptionPane中的文本存储到文本文件中,java,file,io,Java,File,Io,我是个新手。我试图从JOptionPane获取用户输入的文本,并将其存储到文本文件中。此后,我想读一下课文,做些不需要的事情 请帮我保存输入的文本好吗?谢谢 这是我的密码: import javax.swing.JOptionPane; import java.io.*; public class RunProgram { public static void introView() { //The introduction JOptionPane.showMessageD

我是个新手。我试图从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();
        }
    });
}

}试试这个

     public static void personInput() 
     {

        String whatTheUserEntered = JOptionPane.showInputDialog("Enter details");

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory( new File( "./") );
        int actionDialog = chooser.showSaveDialog(yourWindowName); //where the dialog should render
        if (actionDialog == JFileChooser.APPROVE_OPTION)
        {
            File fileName = new File(chooser.getSelectedFile( ) + ".txt" ); //opens a filechooser dialog allowing you to choose where to store the file and appends the .txt mime type
            if(fileName == null)
                return;
            if(fileName.exists()) //if filename already exists
            {
                actionDialog = JOptionPane.showConfirmDialog(yourWindowName,
                                   "Replace existing file?");
                if (actionDialog == JOptionPane.NO_OPTION) //open a new dialog to confirm the replacement file
                    return;
            }
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); 

                    out.write(whatTheUserEntered );
                    out.close(); //write the data to the file and close, please refer to what madProgrammer has explained in the comments here about where the file may not close correctly. 
            }
            catch(Exception ex)
            {
                 System.err.println("Error: " + ex.getMessage());
            }
        }
      }
我基本上是试图从输入对话框中获取文本,并将其写入您选择的文件中。该文件将使用附加字符串“.txt”作为文本文件写入,该字符串将mime类型设置为文本


让我知道进展如何

试试这个

     public static void personInput() 
     {

        String whatTheUserEntered = JOptionPane.showInputDialog("Enter details");

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory( new File( "./") );
        int actionDialog = chooser.showSaveDialog(yourWindowName); //where the dialog should render
        if (actionDialog == JFileChooser.APPROVE_OPTION)
        {
            File fileName = new File(chooser.getSelectedFile( ) + ".txt" ); //opens a filechooser dialog allowing you to choose where to store the file and appends the .txt mime type
            if(fileName == null)
                return;
            if(fileName.exists()) //if filename already exists
            {
                actionDialog = JOptionPane.showConfirmDialog(yourWindowName,
                                   "Replace existing file?");
                if (actionDialog == JOptionPane.NO_OPTION) //open a new dialog to confirm the replacement file
                    return;
            }
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); 

                    out.write(whatTheUserEntered );
                    out.close(); //write the data to the file and close, please refer to what madProgrammer has explained in the comments here about where the file may not close correctly. 
            }
            catch(Exception ex)
            {
                 System.err.println("Error: " + ex.getMessage());
            }
        }
      }
我基本上是试图从输入对话框中获取文本,并将其写入您选择的文件中。该文件将使用附加字符串“.txt”作为文本文件写入,该字符串将mime类型设置为文本


让我知道进展如何

根据您的需要,您有许多潜在的选择

你可以。。。 将内容写入
属性
文件

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);
    }
}
如您所见,您必须亲自实际加载和保存内容。但是,这意味着您可以控制文件的位置

你可以。。。 使用
首选项
API

String name = JOptionPane.showInputDialog("What is your name?");
Preferences preferences = Preferences.userNodeForPackage(RunProgram.class);
preferences.put("user.name", name);
然后你会简单地使用像

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

这样做的好处是存储过程会照顾到您,但您会失去对数据存储位置的控制

你可以。。。
  • 以自己的格式将数据写入文件。这需要大量的工作和开销,但您不仅可以控制文件的位置,还可以控制数据维护的格式。有关更多详细信息,请参阅
  • 以XML格式编写数据,这提供了一定级别的分层控制(如果这很重要的话),但确实增加了管理的复杂性

根据您的需要,您有许多潜在的选择

你可以。。。 将内容写入
属性
文件

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);
    }
}
如您所见,您必须亲自实际加载和保存内容。但是,这意味着您可以控制文件的位置

你可以。。。 使用
首选项
API

String name = JOptionPane.showInputDialog("What is your name?");
Preferences preferences = Preferences.userNodeForPackage(RunProgram.class);
preferences.put("user.name", name);
然后你会简单地使用像

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

这样做的好处是存储过程会照顾到您,但您会失去对数据存储位置的控制

你可以。。。
  • 以自己的格式将数据写入文件。这需要大量的工作和开销,但您不仅可以控制文件的位置,还可以控制数据维护的格式。有关更多详细信息,请参阅
  • 以XML格式编写数据,这提供了一定级别的分层控制(如果这很重要的话),但确实增加了管理的复杂性


它目前做什么?抛出任何错误?@KyleT否,它只是简单地打开joptionpane,然后在上面的目录中创建一个空文本文件…它当前做什么?抛出任何错误?@KyleT否,它只是简单地打开joptionpane,然后在上面的目录中创建一个空文本文件……非常感谢您的回答!我现在正在测试。然而,我只是想知道:chooser.showSaveDialog后面的“yourWindowName”是什么意思?它是jOptionPane在帧内呈现的位置。例如,如果您的框架中心有一个面板,那么您可能希望将joption窗格放置在其顶部。如果您愿意,它也可以为null。有关parentComponent参数的详细信息,请参阅Oracle提供的JOptionPane文档。你有一个潜在的资源,比如因为你正在处理
IOException
的可能性,防止
编写器关闭。@mad程序员哇,我不知道,我今天也学到了一些额外的知识,我一直很感激。非常感谢你的回答!我现在正在测试。然而,我只是想知道:chooser.showSaveDialog后面的“yourWindowName”是什么意思?它是jOptionPane在帧内呈现的位置。例如,如果您的框架中心有一个面板,那么您可能希望将joption窗格放置在其顶部。如果您愿意,它也可以为null。有关parentComponent参数的详细信息,请参阅Oracle提供的JOptionPane文档。你有一个潜在的资源,比如因为你正在处理
IOException
的可能性,防止
编写器关闭。@mad程序员哇,我不知道,我今天也学到了一些额外的知识,我一直很感激。好的,非常感谢你的建议,但是(作为一个新手)我真的不明白这里到底发生了什么…@Chasi先生从其他可能有类似问题的人的角度来看这一点,我相信这是最好的答案,因为它提供了一系列的解决方案。圣诞快乐,伙计们@基本上,我不知道你的绝对长期需求是什么,但我提供了至少四种不同的方法来实现你的要求。你需要权衡每种方法的优缺点,并根据自己的需要确定哪种方法更可取……好的,非常感谢你的建议,但是(作为新手)我真的不明白这里到底发生了什么…@Chasi先生从其他可能有类似问题的人的角度来看这一点,我相信这是最好的答案,因为它提供了一系列的解决方案。圣诞快乐,伙计们@基本上,我不知道你的绝对长期需求是什么,但我提供了至少四种不同的方法来实现你的要求。你需要权衡每种方法的优缺点,并根据自己的需要确定哪种方法更可取。。。