Java Charset create for 8859_1 encoding

Description

Java Charset create for 8859_1 encoding

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class Main {
  public static CharSequence fromFile(String filename) throws IOException {
    FileInputStream fis = new FileInputStream(filename);
    FileChannel fc = fis.getChannel();

    // Create a read-only CharBuffer on the file
    ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
    fis.close();//from  w ww .  j  av a 2 s.c o m
    return cbuf;
  }

  public static void main(String[] argv) throws Exception {
    CharSequence s = fromFile("Main.java");
    System.out.println(s);
  }
}



PreviousNext

Related