얼만큼 읽어와야하는지 length를 정하는 방법을 몰라서
버퍼사이즈를 읽어올만큼만 할당해서 정해놓은 position에서
버퍼사이즈만큼만 읽어옴..-이렇게 짜는거 아니죠?ㅋㅋㅋ
그리고요.. 한글을 인식못해요..숫자랑 영어만..ㅋㅋ
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
String filename2 = "C:\\Documents and Settings\\mina\\test\\Random\\src\\f.txt";
FileChannel outputChannel = null;
FileChannel InputChannel = null;
try {
RandomAccessFile f = new RandomAccessFile(filename,"rw");
RandomAccessFile f2 = new RandomAccessFile(filename2,"rw");
InputChannel = f.getChannel();
outputChannel = f2.getChannel();
int BufferSize = 5;
int BufferStart = 3;
ByteBuffer buf = ByteBuffer.allocate(BufferSize);
InputChannel.position(BufferStart);
InputChannel.read(buf);
buf.flip();
outputChannel.position(BufferStart);
outputChannel.write(buf);
buf.flip();
byte[] by = new byte[buf.limit()];
buf.get(by);
System.out.println(new String(by));
outputChannel.close();
InputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
1 comment:
버퍼 사이즈는 너가 알아서 주면돼. 기본적으로 512, 1024로 주는데..사용자마다 각기 다르다...
Post a Comment