• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange characters on writing a file

 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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;
raf.seek(8);
raf.writeUTF("hi");
raf.close();
}

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
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either Notepad cannot handle UTF characters, or the rest of the file is not in UTF-8 encoding. Try using writeChars instead.
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Thanks
 
Arunav Gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

www.arunav.net
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Arunav Gupta
 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic