Java-如何将.txt文件保存到所需位置
我希望字符串文本保存为一个.txt文件,无论我想在哪里。我认为可以通过使用JFileChooser来实现这一点。无论如何,这是我的代码:Java-如何将.txt文件保存到所需位置,java,swing,file,jfilechooser,Java,Swing,File,Jfilechooser,我希望字符串文本保存为一个.txt文件,无论我想在哪里。我认为可以通过使用JFileChooser来实现这一点。无论如何,这是我的代码: class TextEditorFrame extends JFrame { JFileChooser chooser; public TextEditorFrame(){ Scanner s = new Scanner(System.in); chooser = new JFileChooser();
class TextEditorFrame extends JFrame {
JFileChooser chooser;
public TextEditorFrame(){
Scanner s = new Scanner(System.in);
chooser = new JFileChooser();
String text = s.nextLine();
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))){
out.print(text);
System.out.println("Text Saved:");
System.out.println(text);
} catch (FileNotFoundException ex) {
System.out.println("Something went wrong...");
}
}
}
这是到目前为止我的代码。有没有办法将JFileChooser与字符串文本结合起来
注意:这不是完整的代码,但您需要知道的一切都在这里。这将使打印流进入您所制作的JFileChooser的选定文件
File file = chooser.showSaveDialog();
PrintStream out = new PrintStream(new FileOutputStream(file));
您看过JFileChooser的API了吗?在API中查找JFileChooser并尝试showDialog方法,这样您就可以手动选择保存文件的位置。