本文整理了Java中java.io.RandomAccessFile.getFilePointer
方法的一些代码示例,展示了RandomAccessFile.getFilePointer
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RandomAccessFile.getFilePointer
方法的具体详情如下:
包路径:java.io.RandomAccessFile
类名称:RandomAccessFile
方法名:getFilePointer
[英]Gets the current position within this file. All reads and writes take place at the current file pointer position.
[中]获取此文件中的当前位置。所有读写操作都在当前文件指针位置进行。
代码示例来源:origin: jenkinsci/jenkins
public void skip(long start) throws IOException {
file.seek(file.getFilePointer()+start);
}
代码示例来源:origin: hankcs/HanLP
private int nextId() throws IOException
{
if (raf.length() - raf.getFilePointer() >= 4)
{
int id = raf.readInt();
return id < 0 ? id : table[id];
}
return -2;
}
代码示例来源:origin: robovm/robovm
/**
* Skips over {@code count} bytes in this file. Less than {@code count}
* bytes are skipped if the end of the file is reached or an exception is
* thrown during the operation. Nothing is done if {@code count} is
* negative.
*
* @param count
* the number of bytes to skip.
* @return the number of bytes actually skipped.
* @throws IOException
* if this file is closed or another I/O error occurs.
*/
public int skipBytes(int count) throws IOException {
if (count > 0) {
long currentPos = getFilePointer(), eof = length();
int newCount = (int) ((currentPos + count > eof) ? eof - currentPos : count);
seek(currentPos + newCount);
return newCount;
}
return 0;
}
代码示例来源:origin: normanmaurer/netty-in-action
public void run() throws Exception {
Channel ch = bootstrap.bind(0).sync().channel();
long pointer = 0;
for (;;) {
long len = file.length();
if (len < pointer) {
// file was reset
pointer = len;
} else if (len > pointer) {
// Content was added
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(pointer);
String line;
while ((line = raf.readLine()) != null) {
ch.writeAndFlush(new LogEvent(null, -1,
file.getAbsolutePath(), line));
}
pointer = raf.getFilePointer();
raf.close();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
break;
}
}
}
代码示例来源:origin: apache/flume
SetMultimap<Long, Long> inflights = HashMultimap.create();
if (!fileChannel.isOpen()) {
file = new RandomAccessFile(inflightEventsFile, "rw");
fileChannel = file.getChannel();
if (file.length() == 0) {
return inflights;
file.seek(0);
byte[] checksum = new byte[16];
file.read(checksum);
ByteBuffer buffer = ByteBuffer.allocate(
(int) (file.length() - file.getFilePointer()));
fileChannel.read(buffer);
byte[] fileChecksum = digest.digest(buffer.array());
代码示例来源:origin: tonihele/OpenKeeper
private void readFileContents(File file) throws IOException {
try (RandomAccessFile data = new RandomAccessFile(file, "r")) {
while (data.getFilePointer() < data.length()) {
// Read header (and put the file pointer to the data start)
KwdHeader header = readKwdHeader(data);
readFileContents(header, data);
}
if (data.getFilePointer() != data.length()) {
throw new RuntimeException("Failed to parse file");
}
}
}
代码示例来源:origin: apache/incubator-druid
long lastOffset = 0;
try (final RandomAccessFile raf = new RandomAccessFile(dest, "rw")) {
final int fd = getfd(raf.getFD());
offset = raf.getFilePointer();
numBytes = 0;
代码示例来源:origin: Meituan-Dianping/walle
FileChannel fileChannel = null;
try {
fIn = new RandomAccessFile(apkFile, "rw");
fileChannel = fIn.getChannel();
final long commentLength = ApkUtil.getCommentLength(fileChannel);
fIn.seek(centralDirStartOffset);
fIn.setLength(fIn.getFilePointer());
fIn.seek(fileChannel.size() - commentLength - 6);
代码示例来源: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: atomix/atomix
/**
* Seeks to the given offset.
*/
private void seekToOffset(int offset) throws IOException {
if (randomAccessFile.getFilePointer() != offset) {
randomAccessFile.seek(offset);
}
}
代码示例来源:origin: TeamNewPipe/NewPipe
@Override
public int available() {
try {
return (int) (source.length() - source.getFilePointer());
} catch (IOException ex) {
return 0;
}
}
代码示例来源:origin: ivan-vasilev/neuralnetworks
public byte[] getNextInput()
{
try
{
if (file.length() - file.getFilePointer() < buffer.length)
{
int s = (int) (file.length() - file.getFilePointer());
ByteBuffer bb = ByteBuffer.wrap(buffer, 0, s);
this.file.getChannel().read(bb);
bb = ByteBuffer.wrap(buffer, buffer.length - s, s);
this.file.seek(0);
this.file.getChannel().read(bb);
} else
{
ByteBuffer bb = ByteBuffer.wrap(buffer);
this.file.getChannel().read(bb);
}
} catch (IOException e)
{
e.printStackTrace();
}
return buffer;
}
代码示例来源:origin: jpcsp/jpcsp
private void printLogSettingsFile() {
try {
RandomAccessFile raf = new RandomAccessFile(LogSettingsPath, "r");
while (raf.getFilePointer() < raf.length()) {
settingsArea.append(raf.readLine() + LB);
}
raf.close();
} catch (Exception e) {
settingsArea.setEnabled(false);
}
}
代码示例来源:origin: stackoverflow.com
RandomAccessFile file = new RandomAccessFile("filename.txt", "r");
List<Long> indexList = new ArrayList();
long pos = 0;
while (file.readLine() != null)
{
Long linePos = new Long(pos);
indexList.add(linePos);
pos = file.getFilePointer();
}
int indexSize = indexList.size();
Long[] indexArray = new Long[indexSize];
indexList.toArray(indexArray);
代码示例来源: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.setLength(raf.getFilePointer());
raf.seek(fc.size() - commentLength - 6);
代码示例来源:origin: stackoverflow.com
public void insert(String filename, long offset, byte[] content) {
RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
long fileSize = r.length();
FileChannel sourceChannel = r.getChannel();
FileChannel targetChannel = rtemp.getChannel();
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
sourceChannel.truncate(offset);
r.seek(offset);
r.write(content);
long newOffset = r.getFilePointer();
targetChannel.position(0L);
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
sourceChannel.close();
targetChannel.close();
}
代码示例来源:origin: ltsopensource/light-task-scheduler
private void flushBuf() throws IOException {
if (this.bufDirty) {
if (super.getFilePointer() != this.bufStartPos) {
super.seek(this.bufStartPos);
}
super.write(this.buf, 0, this.bufUsedSize);
this.bufDirty = false;
}
}
代码示例来源:origin: nutzam/nutz
@Override
public int available() throws IOException {
return (int) (raf.length() - raf.getFilePointer());
}
代码示例来源:origin: apache/activemq
public void corruptRecoveryLocation(Location recoveryPosition) throws IOException {
DataFile dataFile = getDataFile(recoveryPosition);
// with corruption on recovery we have no faith in the content - slip to the next batch record or eof
DataFileAccessor reader = accessorPool.openDataFileAccessor(dataFile);
try {
RandomAccessFile randomAccessFile = reader.getRaf().getRaf();
randomAccessFile.seek(recoveryPosition.getOffset() + 1);
byte[] data = new byte[getWriteBatchSize()];
ByteSequence bs = new ByteSequence(data, 0, randomAccessFile.read(data));
int nextOffset = 0;
if (findNextBatchRecord(bs, randomAccessFile) >= 0) {
nextOffset = Math.toIntExact(randomAccessFile.getFilePointer() - bs.remaining());
} else {
nextOffset = Math.toIntExact(randomAccessFile.length());
}
Sequence sequence = new Sequence(recoveryPosition.getOffset(), nextOffset - 1);
LOG.warn("Corrupt journal records found in '{}' between offsets: {}", dataFile.getFile(), sequence);
// skip corruption on getNextLocation
recoveryPosition.setOffset(nextOffset);
recoveryPosition.setSize(-1);
dataFile.corruptedBlocks.add(sequence);
} catch (IOException e) {
} finally {
accessorPool.closeDataFileAccessor(reader);
}
}
代码示例来源:origin: stackoverflow.com
RandomAccessFile first = new RandomAccessFile("D:\\Temp.txt","r");
RandomAccessFile second = new RandomAccessFile("D:\\Temp.txt","r");
String line;
String nextLine = second.readLine();
while (nextLine != null) {
line = first.readLine();
nextLine = second.readLine();
if ((line.contains("[DesiredText]") && nextLine.isEmpty())) {
pozycje.add(first.getFilePointer());
}
}
内容来源于网络,如有侵权,请联系作者删除!