Friday, August 17, 2007

부분읽기

이것도 아니죠?ㅋㅋ..다시 수정할게요..ㅋㅋ

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

public class Part {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
FileChannel InputChannel = null;
long filesize = 0;
int shareNum = 0;
int index = 0;
shareNum = 4;

try {
RandomAccessFile f = new RandomAccessFile(filename,"rw");
InputChannel = f.getChannel();

filesize = f.length();
index = (int) (filesize/shareNum);

int part1 = index+1;
int part2 = index*2+1;
int part3 = index*3+1;

ByteBuffer buf1 = ByteBuffer.allocate(index+1);
InputChannel.position(0);
InputChannel.read(buf1);
buf1.flip();

byte[] by = new byte[buf1.limit()];
buf1.get(by);
System.out.println("1번째 읽어온 부분:" + new String(by));

ByteBuffer buf2 = ByteBuffer.allocate(index);
InputChannel.position(part1);
InputChannel.read(buf2);
buf2.flip();

byte[] by2 = new byte[buf2.limit()];
buf2.get(by2);
System.out.println("2번째 읽어온 부분:" + new String(by2));


ByteBuffer buf3 = ByteBuffer.allocate(index);
InputChannel.position(part2);
InputChannel.read(buf3);
buf3.flip();

byte[] by3 = new byte[buf3.limit()];
buf3.get(by3);
System.out.println("3번째 읽어온 부분:" + new String(by3));

ByteBuffer buf4 = ByteBuffer.allocate(index+1);
InputChannel.position(part3);
InputChannel.read(buf4);
buf4.flip();

byte[] by4 = new byte[buf4.limit()];
buf4.get(by4);
System.out.println("4번째 읽어온 부분:" + new String(by4));

InputChannel.close();

} catch(IOException ex) {
ex.printStackTrace();
}
}
}

No comments: