Hi I am having trouble saving currently open files with JFileChooser. I understand how to save a new file using showSaveDialog but I want to open a file in JFileChooser, edit it and save it to the existing file I have opened instead of it opening a save as menu.
Asked
Active
Viewed 266 times
1 Answers
0
public class EditorFrame extends JFrame {
/**
* @field serialVersionUID
*/
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));
public static void main(String[] args) {
EditorFrame frame = new EditorFrame();
frame.init();
}
private void init(){
this.setSize(640, 480);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Editor");
this.setContentPane(createContentPanel());
this.setVisible(true);
}
/**
* @return
* @return Container
*/
private Container createContentPanel() {
JPanel jPanel = new JPanel(new BorderLayout());
textArea = new JTextArea();
jPanel.add(textArea,BorderLayout.CENTER);
jPanel.add(createButton(),BorderLayout.NORTH);
return jPanel;
}
/**
* @return
* @return Component
*/
private Component createButton() {
JPanel jPanel = new JPanel(new GridLayout(1, 2));
JButton openButton = new JButton("Open");
JButton saveButton = new JButton("Save");
jPanel.add(openButton);
jPanel.add(saveButton);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int state = jfc.showOpenDialog(null);
if (state == 0) {
File f = jfc.getSelectedFile();
try {
String textString = readText(new FileInputStream(f), "utf-8");
textArea.setText(textString);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
});
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
writeText(textArea.getText(),jfc.getSelectedFile().getAbsolutePath());
}
});
return jPanel;
}
private String readText(final InputStream in, String charset) {
if (charset == null) {
charset = Charset.defaultCharset().toString();
}
StringBuffer text = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in, charset));
String line = null;
while ((line = br.readLine()) != null) {
text.append(line + "\n");
}
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return text.toString();
}
private void writeText(final String text, final String descPath) {
File root = new File(descPath.substring(0, descPath.lastIndexOf('\\')));
if (!root.exists()) {
root.mkdirs();
}
try {
// BufferedWriter bw = new BufferedWriter(new
// FileWriter(descPath));
BufferedWriter bw =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(descPath), Charset.forName("UTF-8")));
bw.write(text);
bw.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Kerwin
- 1,212
- 1
- 7
- 14
-
I was saying like I have a Gui that has open,save as and save, but i want to add an action for the menu button save which would just let you save the text file that you just edited without opening up the showSaveDialog. – Jeff Glomez Mar 28 '17 at 06:09
-
For save it, get the exists file path and the new text data , then write the text to file. – Kerwin Mar 28 '17 at 06:58
-
any chance you can give me an example? I tried doing that but it keeps going to the exception. this is what my attempt is ` try{ BufferedWriter fileWriter = new BufferedWriter( new FileWriter(fileChooser.getSelectedFile().getPath())); String textArea = frame.getTextArea().getText(); fileWriter.write(textArea); fileWriter.close(); } catch (Exception ex) { System.out.println("Cannot Save File"); }` – Jeff Glomez Mar 28 '17 at 15:46
-
Which exception, may something you missing. – Kerwin Mar 29 '17 at 02:19