by robert » Thu Jul 22, 2010 12:28 pm
Hi sandeep,
If you use the
TextEditorPane class, it provides handy utility methods for setting the encoding, and loading and saving with that encoding. You use it just like an RSyntaxTextArea. It's just got some extra goodies that make it useful for a true "code editor" application.
- Code: Select all
TextEditorPane textArea = new TextEditorPane();
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
FileLocation file = new FileLocation("C:/temp/test.java");
// null second parameter => use system encoding, if no BOM is found.
// Otherwise this parameter should be the encoding to load with if no BOM.
textArea.load(file, null);
textArea.setEncoding("UTF-8");
textArea.save();
The TextEditorPane.load() and save() methods throw IOExceptions. setEncoding() doesn't, but it sets the dirty flag on the editor (because if you change the encoding and save, the file contents will be different than they were previously).
Alternatively, if you don't want to use TextEditorPane you can just use Java's standard java.io.package to specify the encoding to save with.
- Code: Select all
try {
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:/my/file.txt"), "UTF-8"));
textArea.write(w);
w.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Hi sandeep,
If you use the [url=http://javadoc.fifesoft.com/rsyntaxtextarea/org/fife/ui/rsyntaxtextarea/TextEditorPane.html]TextEditorPane[/url] class, it provides handy utility methods for setting the encoding, and loading and saving with that encoding. You use it just like an RSyntaxTextArea. It's just got some extra goodies that make it useful for a true "code editor" application.
[code]
TextEditorPane textArea = new TextEditorPane();
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
FileLocation file = new FileLocation("C:/temp/test.java");
// null second parameter => use system encoding, if no BOM is found.
// Otherwise this parameter should be the encoding to load with if no BOM.
textArea.load(file, null);
textArea.setEncoding("UTF-8");
textArea.save();
[/code]
The TextEditorPane.load() and save() methods throw IOExceptions. setEncoding() doesn't, but it sets the dirty flag on the editor (because if you change the encoding and save, the file contents will be different than they were previously).
Alternatively, if you don't want to use TextEditorPane you can just use Java's standard java.io.package to specify the encoding to save with.
[code]
try {
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:/my/file.txt"), "UTF-8"));
textArea.write(w);
w.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
[/code]