Java Tutorial/File/Encode Decode

Материал из Java эксперт
Перейти к: навигация, поиск

ISO- Encode Decode

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class MainClass {
  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) "a" });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("ISO-8859-1");
    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();
  }
}
/*
     */



Initial Byte Buffer
0 0 0 0 0 0 0 97 
ISO-8859-1:
0 0 0 97


US-ASCII: Encode Decode

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class MainClass {
  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) "a" });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("US-ASCII");
    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();
  }
}
/*
     */



Initial Byte Buffer
0 0 0 0 0 0 0 97 
US-ASCII:
0 0 0 97


UTF-16BE: Encode Decode

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class MainClass {
  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) "a" });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("UTF-16BE");
    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();
  }
}
/*
   */



Initial Byte Buffer
0 0 0 0 0 0 0 97 
UTF-16BE:
0 0 0 0 0 0 0 97


UTF-16LE: Encode Decode

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class MainClass {
  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) "a" });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("UTF-16LE");
    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();
  }
}
/*
  */



Initial Byte Buffer
0 0 0 0 0 0 0 97 
UTF-16LE:
0 0 0 0 0 0 97 0


UTF-8: Encode Decode

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class MainClass {
  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) "a" });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("UTF-8");
    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();
  }
}
/*
    */



Initial Byte Buffer
0 0 0 0 0 0 0 97 
UTF-8:
0 0 0 97