Wednesday, August 15, 2007

RandomAccessFile--Channel

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Read_Donhak {
public static void main(String[] args) throws FileNotFoundException {
File aFile = new File("c:\\Donhak.txt");
RandomAccessFile inFile = new RandomAccessFile(aFile, "rw");

// 파일 채널 객체
FileChannel inChannel = inFile.getChannel();// ByteBuffer 생성
ByteBuffer buf = ByteBuffer.allocate(1024);

try {
inChannel.read(buf);
byte[] add = buf.array();
String ttt = new String(add);
System.out.println(ttt);
} catch (IOException e) {
e.printStackTrace();
}
buf.clear();
System.exit(0);
}
}

No comments: