This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I agree with Rob's comments. I suggest you to use Editplus instead of Notepad.
You have to change file encoding in Editplus using Documnet > File Encoding > UTF-8
Any Test editor should be fine other than note pad
Joined: Mar 19, 2009
Hi Rob and Srinivas
Thanks for your replies.
@Rob I tried with writeChars but it is also giving the same thing.
@Srinivas I will try with editplus and check it out.
If you want to create, write and read the files in utf-8 format through your program you can surely use the writeUTF and readUTF functions of RandomAccessFile object, however if you try to open such a file created by your program in notepad you will find all sorts of special characters (particularly at the line breaks) between human readable text.
Also do not confuse with the UTF-8 file type option of the notepad, if you create a file using notepad and save it as utf-8 encoding the file would save with 3 extra bytes of information prefixed to the beginning of the file content, which is purely for the editor to identify the encoding of the file while displaying it. These 3 extra bytes at the beginning of the file which can prevent RandomAccessFile object from performing writeUTF or readUTF correctly. which is also the case for writeChars (it also requires 2 bytes to represent each character).
In summary if you want to create, read and write file only through your program use writeUTF and readUTF.
But incase if you want to be able to view such file in notepad and you do not plan to use characters beyond the ascii character set, then probably you should go with a normal ascii encoding using the writeBytes and readLine function of RandomAccessFile object to write and read text, which takes 1 byte per character (also ASCII encoding does not need any prefixed information in the file to be understood by the notepad, since it is the default encoding for the notepad).
Finally, based on the value you are expecting in the file you probably want to do seek(7) and not seek(8) since the seek function reads the number of given bytes and places the file pointer to the next position.