Java初心者です。JFileChooser で showSaveDialoge() を使用してファイルにコンテンツを書き込む簡単なプログラムを作成しました。以下を含むコード。
public static void main(String arg[]) throws IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
JFrame frame = new JFrame();
JFileChooser fc = new JFileChooser();
try {
File file = new File("fileName.txt");
fc.setSelectedFile(file);
int r = fc.showSaveDialog(frame);
if(r == JFileChooser.APPROVE_OPTION)
{
FileWriter writer = new FileWriter(file);
writer.append("Data inside the file");
writer.flush();
writer.close();
}
else if(r == JFileChooser.CANCEL_OPTION) {
System.out.println("Do nothing for CANCEL");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "File could not be written, try again.");
}
}
コードが実行され、保存ダイアログ ボックスが表示されます。しかし、ダイアログ ボックスの [保存] ボタンをクリックしても、何も起こりませんでした。ファイルは選択した場所に保存されていません。何が原因でしょうか。前もって感謝します。