本文整理了Java中java.io.RandomAccessFile.seek
方法的一些代码示例,展示了RandomAccessFile.seek
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RandomAccessFile.seek
方法的具体详情如下:
包路径:java.io.RandomAccessFile
类名称:RandomAccessFile
方法名:seek
[英]Moves this file's file pointer to a new position, from where following read, write or skip operations are done. The position may be greater than the current length of the file, but the file's length will only change if the moving of the pointer is followed by a write operation.
[中]将此文件的文件指针移动到一个新位置,从该位置执行以下读取、写入或跳过操作。该位置可能大于文件的当前长度,但只有在指针移动后进行写入操作时,文件的长度才会改变。
代码示例来源:origin: Tencent/tinker
@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
synchronized (sharedRaf) {
final long length = endOffset - offset;
if (byteCount > length) {
byteCount = (int) length;
}
sharedRaf.seek(offset);
int count = sharedRaf.read(buffer, byteOffset, byteCount);
if (count > 0) {
offset += count;
return count;
} else {
return -1;
}
}
}
@Override public long skip(long byteCount) throws IOException {
代码示例来源:origin: osmdroid/osmdroid
GEMFInputStream(final String filePath, final long offset, final int length) throws IOException {
this.raf = new RandomAccessFile(filePath, "r");
raf.seek(offset);
this.remainingBytes = length;
}
代码示例来源:origin: CarGuo/GSYVideoPlayer
static void modify(File file) throws IOException {
long size = file.length();
if (size == 0) {
recreateZeroSizeFile(file);
return;
}
RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
accessFile.seek(size - 1);
byte lastByte = accessFile.readByte();
accessFile.seek(size - 1);
accessFile.write(lastByte);
accessFile.close();
}
代码示例来源:origin: stackoverflow.com
try{
String fileName = "c:/myraffile.txt";
File file = new File(fileName);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.readChar();
raf.seek(0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代码示例来源:origin: alibaba/fescar
private void initFile(String fullFileName) throws IOException {
this.currFullFileName = fullFileName;
this.hisFullFileName = fullFileName + HIS_DATA_FILENAME_POSTFIX;
try {
currDataFile = new File(currFullFileName);
if (!currDataFile.exists()) {
currDataFile.createNewFile();
trxStartTimeMills = System.currentTimeMillis();
} else {
trxStartTimeMills = currDataFile.lastModified();
}
currRaf = new RandomAccessFile(currDataFile, "rw");
currRaf.seek(currDataFile.length());
currFileChannel = currRaf.getChannel();
} catch (IOException exx) {
LOGGER.error("init file error," + exx.getMessage());
throw exx;
}
}
代码示例来源:origin: apache/ignite
/**
* @throws Exception If failed.
*/
@Test
public void testReadLineFromBinaryFile() throws Exception {
File file = new File(FILE_PATH);
file.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
byte[] b = new byte[100];
Arrays.fill(b, (byte)10);
raf.write(b);
raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" +
"b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl());
raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" +
"b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl());
raf.write(b);
raf.writeBytes("test" + U.nl());
raf.getFD().sync();
raf.seek(0);
while (raf.getFilePointer() < raf.length()) {
String s = raf.readLine();
X.println("String: " + s + ";");
X.println("String length: " + s.length());
X.println("File pointer: " + raf.getFilePointer());
}
}
代码示例来源:origin: voldemort/voldemort
public static void main(String[] args) throws Exception {
if(args.length != 1)
Utils.croak("USAGE: java " + CatReadOnlyStore.class.getName() + " directory");
File dir = new File(args[0]);
Serializer<Object> serializer = new JsonTypeSerializer(JsonTypeDefinition.fromJson("'string'"),
true);
for(int chunk = 0;; chunk++) {
File indexFile = new File(dir, chunk + ".index");
File dataFile = new File(dir, chunk + ".data");
if(!indexFile.exists() || !dataFile.exists())
break;
RandomAccessFile index = new RandomAccessFile(indexFile, "r");
RandomAccessFile data = new RandomAccessFile(dataFile, "r");
position = index.readInt();
data.seek(position);
int size = data.readInt();
byte[] value = new byte[size];
代码示例来源:origin: jenkinsci/jenkins
public Void invoke(File f, VirtualChannel channel) throws IOException {
try (OutputStream os = p.getOut();
OutputStream out = new java.util.zip.GZIPOutputStream(os, 8192);
RandomAccessFile raf = new RandomAccessFile(reading(f), "r")) {
raf.seek(offset);
byte[] buf = new byte[8192];
int len;
while ((len = raf.read(buf)) >= 0) {
out.write(buf, 0, len);
}
return null;
}
}
});
代码示例来源:origin: smuyyh/BookReader
RandomAccessFile raf = new RandomAccessFile(file, "rw");
if (skip < 0 || skip > raf.length()) {
System.out.println("skip error");
return;
raf.setLength(raf.length() + b.length);
for (long i = raf.length() - 1; i > b.length + skip - 1; i--) {
raf.seek(i - b.length);
byte temp = raf.readByte();
raf.seek(i);
raf.writeByte(temp);
raf.seek(skip);
raf.write(b);
raf.close();
} catch (Exception e) {
e.printStackTrace();
代码示例来源:origin: stackoverflow.com
class WidgetList extends AbstractList<Widget> {
private static final int SIZE_OF_WIDGET = 100;
private final RandomAccessFile file;
public WidgetList(RandomAccessFile file) {
this.file = file;
}
@Override
public int size() {
return (int)(file.length() / SIZE_OF_WIDGET);
}
@Override
public Widget get(int index) {
file.seek((long)index * SIZE_OF_WIDGET);
byte[] data = new byte[SIZE_OF_WIDGET];
file.read(data);
return new Widget(data);
}
}
代码示例来源:origin: apache/incubator-druid
/**
* Open a stream to a file.
*
* @param offset If zero, stream the entire log. If positive, read from this byte position onwards. If negative,
* read this many bytes from the end of the file.
*
* @return input supplier for this log, if available from this provider
*/
public static InputStream streamFile(final File file, final long offset) throws IOException
{
final RandomAccessFile raf = new RandomAccessFile(file, "r");
final long rafLength = raf.length();
if (offset > 0) {
raf.seek(offset);
} else if (offset < 0 && offset < rafLength) {
raf.seek(Math.max(0, rafLength + offset));
}
return Channels.newInputStream(raf.getChannel());
}
}
代码示例来源:origin: apache/ignite
/** */
private void corruptPartition(File partitionsDir) throws IOException {
ThreadLocalRandom rand = ThreadLocalRandom.current();
for(File partFile : partitionsDir.listFiles((d, n) -> n.startsWith("part"))) {
try (RandomAccessFile raf = new RandomAccessFile(partFile, "rw")) {
byte[] buf = new byte[1024];
rand.nextBytes(buf);
raf.seek(4096 * 2 + 1);
raf.write(buf);
}
}
}
代码示例来源:origin: apache/geode
public synchronized void reopen(long lastPosition) throws IOException {
if (isClosed) {
throw new IOException("Random Access File is closed");
}
try {
this.raf.close();
} catch (IOException e) {
// ignore
}
this.raf = new RandomAccessFile(file, mode);
this.raf.seek(lastPosition);
}
代码示例来源:origin: stackoverflow.com
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine();
} catch (Exception e) {}
reader.seek(0);
load = reader.readLine();
reader.close();
代码示例来源:origin: jenkinsci/jenkins
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
long len = raf.length();
raf.seek(pos);
代码示例来源:origin: internetarchive/heritrix3
StringBuffer sb = new StringBuffer();
try {
endPos = raf.length();
lastPos = endPos;
raf.seek(endPos - 1);
raf.read(oneByte);
if ((char) oneByte[0] != '\n') {
numOfLines++;
pos = lastPos - BUFFERSIZE;
raf.seek(pos);
raf.seek(pos);
if ((endPos - pos) < BUFFERSIZE) {
int remainer = (int) (endPos - pos);
info = buildDisplayingHeader(sb.length(), raf.length());
} catch (FileNotFoundException e) {
sb = null;
try {
if (raf != null) {
raf.close();
代码示例来源:origin: square/tape
@Test public void testNegativeSizeInHeaderThrows() throws IOException {
RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd");
emptyFile.seek(0);
emptyFile.writeInt(-2147483648);
emptyFile.setLength(INITIAL_LENGTH);
emptyFile.getChannel().force(true);
emptyFile.close();
try {
newQueueFile();
fail("Should have thrown about bad header length");
} catch (IOException ex) {
assertThat(ex.getMessage()).isIn(
Arrays.asList("File is corrupt; length stored in header (-2147483648) is invalid.",
"Unable to read version 0 format. Supported versions are 1 and legacy."));
}
}
代码示例来源:origin: mcxiaoke/packer-ng-plugin
FileChannel fc = null;
try {
raf = new RandomAccessFile(apkFile, "rw");
fc = raf.getChannel();
final long commentLength = ApkUtil.findZipCommentLength(fc);
raf.seek(centralDirStartOffset);
final byte[] centralDirBytes = new byte[(int) (fc.size() - centralDirStartOffset)];
raf.read(centralDirBytes);
raf.write(centralDirBytes);
raf.seek(fc.size() - commentLength - 6);
raf.write(temp.array());
代码示例来源:origin: aragozin/jvm-tools
LongBuffer revertBuffer() throws IOException {
LongBuffer reverted = new LongBuffer(buffer.length);
if (bufferSize < buffer.length) {
for (int i=0;i<bufferSize;i++) {
reverted.writeLong(buffer[bufferSize - 1 - i]);
}
} else {
writeStream.flush();
RandomAccessFile raf = new RandomAccessFile(backingFile,"r");
long offset = raf.length();
while(offset > 0) {
offset-=8;
raf.seek(offset);
reverted.writeLong(raf.readLong());
}
}
reverted.startReading();
return reverted;
}
代码示例来源:origin: czy1121/update
private int copy(InputStream in, RandomAccessFile out) throws IOException, UpdateError {
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream bis = new BufferedInputStream(in, BUFFER_SIZE);
try {
out.seek(out.length());
int bytes = 0;
long previousBlockTime = -1;
while (!isCancelled()) {
int n = bis.read(buffer, 0, BUFFER_SIZE);
if (n == -1) {
break;
}
out.write(buffer, 0, n);
bytes += n;
checkNetwork();
if (mSpeed != 0) {
previousBlockTime = -1;
} else if (previousBlockTime == -1) {
previousBlockTime = System.currentTimeMillis();
} else if ((System.currentTimeMillis() - previousBlockTime) > TIME_OUT) {
throw new UpdateError(UpdateError.DOWNLOAD_NETWORK_TIMEOUT);
}
}
return bytes;
} finally {
out.close();
bis.close();
in.close();
}
}
内容来源于网络,如有侵权,请联系作者删除!