您的位置:首页 > 编程语言 > Java开发

java的seek()函数

2016-05-16 22:14 661 查看
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Text {

public static void main(String[] args) throws IOException {

long pos=3l;

RandomAccessFile rad=new RandomAccessFile("E://demo.txt", "r");

rad.seek(pos);

rad.readInt();

rad.readShort();

}

}

关于sekk() 的源代码:

/**

* Sets the file-pointer offset, measured from the beginning of this

* file, at which the next read or write occurs. The offset may be

* set beyond the end of the file. Setting the offset beyond the end

* of the file does not change the file length. The file length will

* change only by writing after the offset has been set beyond the end

* of the file.

*

* @param pos the offset position, measured in bytes from the

* beginning of the file, at which to set the file

* pointer.

* @exception IOException if <code>pos</code> is less than

* <code>0</code> or if an I/O error occurs.

*/

大概的意思是:跳到pos的位置,以字节为单位

readint 的用法,返回四个字节长度的数据即返回一个int的值,指针自动跳过四个字节,源代码如下:

/**

* Reads a signed 32-bit integer from this file. This method reads 4

* bytes from the file, starting at the current file pointer.

* If the bytes read, in order, are <code>b1</code>,

* <code>b2</code>, <code>b3</code>, and <code>b4</code>, where

* <code>0 <= b1, b2, b3, b4 <= 255</code>,

* then the result is equal to:

* <blockquote><pre>

* (b1 << 24) | (b2 << 16) + (b3 << 8) + b4

* </pre></blockquote>

* <p>

* This method blocks until the four bytes are read, the end of the

* stream is detected, or an exception is thrown.

*

* @return the next four bytes of this file, interpreted as an

* <code>int</code>.

* @exception EOFException if this file reaches the end before reading

* four bytes.

* @exception IOException if an I/O error occurs.

*/

public final int readInt() throws IOException {

int ch1 = this.read();

int ch2 = this.read();

int ch3 = this.read();

int ch4 = this.read();

if ((ch1 | ch2 | ch3 | ch4) < 0)

throw new EOFException();

return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));

}

eadShort();跟上面一样,返回两个字节的数,即返回一个short的值。源码如下:

/**

* Reads a signed 16-bit number from this file. The method reads two

* bytes from this file, starting at the current file pointer.

* If the two bytes read, in order, are

* <code>b1</code> and <code>b2</code>, where each of the two values is

* between <code>0</code> and <code>255</code>, inclusive, then the

* result is equal to:

* <blockquote><pre>

* (short)((b1 << 8) | b2)

* </pre></blockquote>

* <p>

* This method blocks until the two bytes are read, the end of the

* stream is detected, or an exception is thrown.

*

* @return the next two bytes of this file, interpreted as a signed

* 16-bit number.

* @exception EOFException if this file reaches the end before reading

* two bytes.

* @exception IOException if an I/O error occurs.

*/

public final short readShort() throws IOException {

int ch1 = this.read();

int ch2 = this.read();

if ((ch1 | ch2) < 0)

throw new EOFException();

return (short)((ch1 << 8) + (ch2 << 0));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: