如何允许用户在Java中创建自己选择的新.txt文件

如何允许用户在Java中创建自己选择的新.txt文件,java,file,input,formatter,Java,File,Input,Formatter,我试图编写的代码有一个jTextField供用户输入字符串。然后,用户将单击jButton来创建文件。对于文件的创建,我想到了:从用户处获取字符串输入,并在文件创建方法中将其附加到.txt中。下面的代码显示了此失败的尝试: String input; Scanner scan = new Scanner(System.in); input = scan; //This is totally wrong, but I'm not sure how to convert the scanner p

我试图编写的代码有一个jTextField供用户输入字符串。然后,用户将单击jButton来创建文件。对于文件的创建,我想到了:从用户处获取字符串输入,并在文件创建方法中将其附加到.txt中。下面的代码显示了此失败的尝试:

String input;
Scanner scan = new Scanner(System.in);
input = scan; //This is totally wrong, but I'm not sure how to convert the scanner

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    final Formatter x;
    try{

      x = new Formatter(input + ".txt");

    } catch(Exception e) {
        System.out.println("Your code is bad. You should feel bad.");
    }
}           

 private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
     input = jTextField1.getText();         
}        

没关系,我为我的案子找到了最好的解决办法。不过,谢谢

  final Formatter x;
    try{

        x = new Formatter(jTextField1.getText() + ".txt");

    } catch(Exception e) {
        System.out.println("Something went horribly wrong..");
    }
}                           

结果是一个null.txt,而不是用户的string.txt文件!在我的追求中,我是一个糟糕的程序员,哈哈