Javaファイルの読み込み
2410 ワード
1.RandomAccessFileオブジェクトのseek関数を使用して実現
public void seek(long pos) throws IOException
1
2
3
4
public void seek(long pos) throws IOException
1
2
3
4
RandomAccessFile randomAccessFile =
new
RandomAccessFile (
"./chanel.txt"
,
"r"
);
// Set the file position 10
randomAccessFile.seek (
10
);
System.out.println(randomAccessFile.readLine());
ここでposはオフセット 、pos=0、ファイル 、pos=10はオフセットファイル の10 を す
バイト .バイト であることに してください.ASCIIコードは、「A」、「a」などのバイトを す. は2バイトを します.
の はEditPlusで、1バイトがcolを めていることを しています.
2.FileChannelのposition を いて
public abstract FileChannel position(long newPosition) throws IOException
このうちnewPositionは のposと じ です.
FileChannelを する に く があります.しかし、FileChannelを くことはできません.
InputStream、OutputStream、またはRandomAccessFileを する があります.
FileChannelインスタンスを します. に、RandomAccessFileでFileChannelを く を します.
1
2
3
4
5 RandomAccessFile randomAccessFile =
new
RandomAccessFile (
"./chanel.txt"
,
"r"
);
FileChannel fileChannel = randomAccessFile.getChannel( );
//Set the file position 10
fileChannel.position (
10
);
System.out.println(randomAccessFile.readLine());