jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Strange characters on writing a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strange characters on writing a file" Watch "Strange characters on writing a file" New topic

Strange characters on writing a file

utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57

I am writing a file using RandomAccessFile. On opening the file in notepad I am seeing strange characters written. Please help.

initial content of file: 000000000000

Ater executing the following code.

public static void main(String[] args) throws Exception{
File f = new File("c:\\raf.txt");
RandomAccessFile raf = new RandomAccessFile(f,"rw");
int i=0;

Final content of file: 000Ȁ楨0000000

what I am expecting : 0000000hi000

This small thing has become a huge Bug in my application. Please help !
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Either Notepad cannot handle UTF characters, or the rest of the file is not in UTF-8 encoding. Try using writeChars instead.

How To Ask Questions How To Answer Questions
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
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
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
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.

Arunav Gupta

Joined: Dec 23, 2009
Posts: 1
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.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to JavaRanch, Arunav Gupta
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi Arunav

Thank you so much for the great amount of information.
I've figured a solution by using writeBytes for the file and opening it in UTF8 56001 encoding using editplus.

Thanks to all once again!
I agree. Here's the link: http://aspose.com/file-tools
subject: Strange characters on writing a file
It's not a secret anymore!