I have text file that was encoded with UTF8 (for language specific characters). I need to use RandomAccessFile to seek specific position and read from.
I want read line-by-line.
String str = myreader.readLine(); //returns wrong text, not decoded
String str myreader.readUTF(); //An exception occurred: java.io.EOFException
UTF-8 is not a character set but an encoding used with Unicode. It happens to be compatible with ASCII too, because the codes used for multiple byte encodings lie in the part of the ASCII character set that is unused.
UTF-8 is an 8-bit variable width encoding. The first 128 characters in the Unicode, when represented with UTF-8 encoding have the representation as the characters in ASCII.
RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
You can convert string, read by readLine to UTF8, using following code:
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile(new File("MyFile.txt"), "r");
String line = raf.readLine();
String utf8 = new String(line.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("Line: " + line);
System.out.println("UTF8: " + utf8);
}
Привет из Украины
Line: ÐÑÐ¸Ð²ÐµÑ Ð¸Ð· УкÑаинÑ
UTF8: Привет из Украины
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With