Para ler:
private void loadFile() {
StringBuilder conteudo = new StringBuilder(500);
try {
if (selectedFile.exists()) {
InputStreamReader is = new InputStreamReader(new FileInputStream(selectedFile), Charset.forName("iso-8859-1"));
BufferedReader reader = new BufferedReader(is);
String linha = null;
while ((linha = reader.readLine()) != null) {
conteudo.append(linha).append(System.getProperty("line.separator"));
}
reader.close();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
jTextPaneContent.setText(conteudo.toString());
}
Para gravar:
private void saveFile() {Código fonte contendo um exemplo funcional.
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(selectedFile), Charset.forName("iso-8859-1"));
osw.write(jTextPaneContent.getText());
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
Complemento:
ResponderExcluirhttp://stackoverflow.com/questions/1749064/how-to-find-default-charset-encoding-in-java