Here is an example
private void doOpenFile() {
int result = myFileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
Path path = myFileChooser.getSelectedFile().toPath();
try {
String contentString = "";
for (String s : Files.readAllLines(path, StandardCharsets.UTF_8)) {
contentString += s;
}
jText.setText(contentString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void doSaveFile() {
int result = myFileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// We'll be making a mytmp.txt file, write in there, then move it to
// the selected
// file. This takes care of clearing that file, should there be
// content in it.
File targetFile = myFileChooser.getSelectedFile();
try {
if (!targetFile.exists()) {
targetFile.createNewFile();
}
FileWriter fw = new FileWriter(targetFile);
fw.write(jText.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}