Java - 如何将 .txt 文件保存到您想要的位置
Java - How to save a .txt file where you want
我希望将字符串 text
另存为 .txt 文件。我认为您可以使用 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 与字符串文本结合起来吗?
注意:这不是完整的代码,但您需要知道的一切都在这里。
这会使 PrintStream 进入您创建的 JFileChooser 的选定文件。
File file = chooser.showSaveDialog();
PrintStream out = new PrintStream(new FileOutputStream(file));
我希望将字符串 text
另存为 .txt 文件。我认为您可以使用 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 与字符串文本结合起来吗?
注意:这不是完整的代码,但您需要知道的一切都在这里。
这会使 PrintStream 进入您创建的 JFileChooser 的选定文件。
File file = chooser.showSaveDialog();
PrintStream out = new PrintStream(new FileOutputStream(file));