Changing file encoding

Post a reply

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Topic review
   

Expand view Topic review: Changing file encoding

Re: Changing file encoding

Post by vijay » Tue Jan 15, 2013 3:34 pm

Thankyou so much ..... robert

Re: Changing file encoding

Post by sandeep » Fri Jul 23, 2010 5:04 pm

Thanx its working fine now.....

Re: Changing file encoding

Post by robert » Thu Jul 22, 2010 6:09 pm

It should be UTF-8, it's just that Java does not write out a BOM when writing UTF-8 files. If you write non-ASCII characters, you'll see that they were saved correctly.

You can use org.fife.io.UnicodeWriter if you want to force writing of a BOM. UnicodeWriter (and companion UnicodeReader) are part of RSyntaxTextArea. This is actually what TextEditorPane is doing under the covers.

Code: Select all
// Tell UnicodeWriter to always write BOMs.
System.setProperty(UnicodeWriter.PROPERTY_WRITE_UTF8_BOM, "true");
BufferedWriter bw = new BufferedWriter(new UnicodeWriter("C:\\test.txt", "UTF-8"));
bw.write("sandeep");
bw.close();


The "UTF-8" as the second parameter in the UnicodeWriter constuctor tells it what encoding to use. If this is "UTF-8", "UTF-16", "UTF-16BE", etc., it'll write out the appropriate BOM first.

Re: Changing file encoding

Post by sandeep » Thu Jul 22, 2010 4:15 pm

I have written following code, but still it write file in ANSI Encoding.

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("C:\\test.txt"), "UTF-8"));
bw.write("sandeep");
bw.close();


In between i will try texteditorpane......

Re: Changing file encoding

Post 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();
}

Changing file encoding

Post by sandeep » Thu Jul 22, 2010 10:54 am

How to change file encoding to UTF-8, is there any method in RSTA to do so.

Top