Shoken Startup Blog

KitchHike Founder/CTO

JFileChooserを使ってTextAreaの内容をファイル名を記入して保存

JFileChooserを使う。保存するファイルの拡張子はtxt。

JFileChooser fileChooser = new JFileChooser;
JTextArea tarea = new JTextArea("この内容が保存されます。");

void saveAs() {
  int x = fileChooser.showSaveDialog(this);
    try {
     if (x == JFileChooser.APPROVE_OPTION) {
	FileWriter fw = new FileWriter(fileChooser.getSelectedFile() + ".txt");
	 fw.write(tarea.getText());
	 fw.close();
	}
    } catch(Exception ex) {
	ex.printStackTrace();
    }
}