Use File Channel to read byte data into a byte buffer and convert byte data into character data - Java File Path IO

Java examples for File Path IO:File Channel

Description

Use File Channel to read byte data into a byte buffer and convert byte data into character data

Demo Code

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

public class Main {
  public static void main(String[] arguments) {
    try {// w  w w.j ava 2s.c o m
      // read byte data into a byte buffer
      String data = "friends.dat";
      FileInputStream inData = new FileInputStream(data);
      FileChannel inChannel = inData.getChannel();
      long inSize = inChannel.size();
      ByteBuffer source = ByteBuffer.allocate((int) inSize);
      inChannel.read(source, 0);
      source.position(0);
      System.out.println("Original byte data:");
      for (int i = 0; source.remaining() > 0; i++) {
        System.out.print(source.get() + " ");
      }
      // convert byte data into character data
      source.position(0);
      Charset ascii = Charset.forName("US-ASCII");
      CharsetDecoder toAscii = ascii.newDecoder();
      CharBuffer destination = toAscii.decode(source);
      destination.position(0);
      System.out.println("\n\nNew character data:");
      for (int i = 0; destination.remaining() > 0; i++) {
        System.out.print(destination.get());
      }
      System.out.println();
    } catch (FileNotFoundException fne) {
      System.out.println(fne.getMessage());
    } catch (IOException ioe) {
      System.out.println(ioe.getMessage());
    }
  }
}

Result


Related Tutorials