Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Encoding Issue: Change UTF8 with BOM char file to UTF16LE without BOM char.

807589Jul 7 2008 — edited Jul 7 2008
i am trying to read UTF 8 file with BOM char .. if BOM mark present then i want to remove that BOM char
and write same file with UTF16LE with out BOM char ..
Please suggest solution on this .
FileInputStream fis = new FileInputStream(file);
			long size = file.length();
			byte[] b = new byte[(int) size];
			int bytesRead = fis.read(b, 0, (int) size);
			if (bytesRead != size) {
 				throw new IOException("cannot read file");
			}
			byte[] srcBytes = b;
                        int b0 = srcBytes[0] & 0xff;
			int b1 = srcBytes[1] & 0xff;
			int b2 = srcBytes[2] & 0xff;
			int b3 = srcBytes[3] & 0xff;
			if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf) {
				System.out.println("Hint: the file starts with a UTF-8 BOM.");
			} 
                         String srcStr = new String(b ,"UTF8");
  			String 	encoding= "UnicodeLittle";
                        writeFile(filePath, srcStr,encoding);// Here is writing file with UTF16LE
	
But files gets written with BOM char .
how do i remove this .
Please suggest solution on this
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2008
Added on Jul 7 2008
4 comments
1,468 views