本文整理了Java中java.io.RandomAccessFile.length
方法的一些代码示例,展示了RandomAccessFile.length
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RandomAccessFile.length
方法的具体详情如下:
包路径:java.io.RandomAccessFile
类名称:RandomAccessFile
方法名:length
[英]Returns the length of this file in bytes.
[中]返回此文件的长度(字节)。
代码示例来源:origin: libgdx/libgdx
public long length () {
try {
if (!exists()) {
return 0;
}
RandomAccessFile raf = new RandomAccessFile(this, "r");
long len = raf.length();
raf.close();
return len;
} catch (IOException e) {
return 0;
}
}
代码示例来源:origin: stackoverflow.com
import java.io.RandomAccessFile;
RandomAccessFile f = new RandomAccessFile(fileName, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
代码示例来源:origin: apache/storm
private byte[] readFile(File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] buffer = new byte[(int) raf.length()];
raf.readFully(buffer);
raf.close();
return buffer;
}
代码示例来源:origin: Codecademy/EventHub
@Override
public DmaIdList build(String filename) {
try {
File file = new File(filename);
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
try (RandomAccessFile raf = new RandomAccessFile(new File(filename), "rw")) {
raf.setLength(DmaIdList.META_DATA_SIZE + defaultCapacity * DmaIdList.SIZE_OF_DATA);
}
}
try (RandomAccessFile raf = new RandomAccessFile(new File(filename), "rw")) {
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
int numRecords = buffer.getInt();
int capacity = (int) (raf.length() - DmaIdList.META_DATA_SIZE) / DmaIdList.SIZE_OF_DATA;
buffer.position(DmaIdList.META_DATA_SIZE + numRecords * DmaIdList.SIZE_OF_DATA);
return new DmaIdList(filename, buffer, numRecords, capacity);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
代码示例来源:origin: GitLqr/LQRWeChat
File f = new File(path);
RandomAccessFile raf = null;
try {
if (f.exists()) {
if (!append) {
f.delete();
raf = new RandomAccessFile(f, "rw");
raf.seek(raf.length());
raf.write(content);
res = true;
代码示例来源:origin: alibaba/fescar
@Override
public boolean hasRemaining(boolean isHistory) {
File file = null;
RandomAccessFile raf = null;
long currentOffset = 0;
if (isHistory) {
file = new File(hisFullFileName);
currentOffset = recoverHisOffset;
} else {
file = new File(currFullFileName);
currentOffset = recoverCurrOffset;
}
try {
raf = new RandomAccessFile(file, "r");
return currentOffset < raf.length();
} catch (IOException exx) {
} finally {
closeFile(raf);
}
return false;
}
代码示例来源:origin: libgdx/libgdx
void addPatchFile(String zipFileName) throws IOException {
File file = new File(zipFileName);
RandomAccessFile f = new RandomAccessFile(file, "r");
long fileLength = f.length();
f.close();
throw new java.io.IOException();
f.seek(0);
f.seek(searchStart);
ByteBuffer bbuf = ByteBuffer.allocate((int) readAmount);
byte[] buffer = bbuf.array();
MappedByteBuffer directoryMap = f.getChannel().map(
FileChannel.MapMode.READ_ONLY, dirOffset, dirSize);
directoryMap.order(ByteOrder.LITTLE_ENDIAN);
代码示例来源: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: 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: apache/storm
/**
* Given a zip File input it will return its size Only works for zip files whose uncompressed size is less than 4 GB, otherwise returns
* the size module 2^32, per gzip specifications
*
* @param myFile The zip file as input
* @return zip file size as a long
*
* @throws IOException
*/
public static long zipFileSize(File myFile) throws IOException {
try (RandomAccessFile raf = new RandomAccessFile(myFile, "r")) {
raf.seek(raf.length() - 4);
long b4 = raf.read();
long b3 = raf.read();
long b2 = raf.read();
long b1 = raf.read();
return (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
}
}
代码示例来源: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/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: Alluxio/alluxio
/**
* Constructs a Block reader given the file path of the block.
*
* @param path file path of the block
*/
public LocalFileBlockReader(String path) throws IOException {
mFilePath = Preconditions.checkNotNull(path, "path");
mLocalFile = mCloser.register(new RandomAccessFile(mFilePath, "r"));
mFileSize = mLocalFile.length();
mLocalFileChannel = mCloser.register(mLocalFile.getChannel());
}
代码示例来源:origin: net.java.dev.jna/jna
private void runDetection() throws IOException {
RandomAccessFile raf = new RandomAccessFile(filename, "r");
try {
if (raf.length() > 4) {
byte[] magic = new byte[4];
raf.seek(0);
raf.read(magic);
if (Arrays.equals(magic, ELF_MAGIC)) {
ELF = true;
return;
raf.seek(4);
_64Bit = sizeIndicator == EI_CLASS_64BIT;
bigEndian = endianessIndicator == EI_DATA_BIG_ENDIAN;
raf.seek(0);
raf.getChannel().read(headerData, 0);
raf.close();
} catch (IOException ex) {
代码示例来源: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: jenkinsci/jenkins
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
long len = raf.length();
raf.seek(pos);
代码示例来源:origin: checkstyle/checkstyle
/**
* Checks whether the content provided by the Reader ends with the platform
* specific line separator.
* @param randomAccessFile The reader for the content to check
* @return boolean Whether the content ends with a line separator
* @throws IOException When an IO error occurred while reading from the
* provided reader
*/
private boolean endsWithNewline(RandomAccessFile randomAccessFile)
throws IOException {
final boolean result;
final int len = lineSeparator.length();
if (randomAccessFile.length() < len) {
result = false;
}
else {
randomAccessFile.seek(randomAccessFile.length() - len);
final byte[] lastBytes = new byte[len];
final int readBytes = randomAccessFile.read(lastBytes);
if (readBytes != len) {
throw new IOException("Unable to read " + len + " bytes, got "
+ readBytes);
}
result = lineSeparator.matches(lastBytes);
}
return result;
}
代码示例来源: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 raf = new RandomAccessFile("path to ur delimited file", "rw");
FileChannel fileChannel = raf.getChannel();
raf.readLine();
raf.seek(raf.getFilePointer());
int len = (int) (raf.length() - raf.getFilePointer());
byte[] bytearr = new byte[len];
raf.readFully(bytearr, 0, len);
fileChannel.truncate(0);
raf.write(bytearr,0,len);
代码示例来源:origin: apache/ignite
/**
* @param name File path.
* @param size Expected file size. If size is 0, the existing size will be used.
* @throws IOException If failed to open the file or memory-map it.
*/
public MappedFile(File name, long size) throws IOException {
file = new RandomAccessFile(name, "rw");
try {
if (size == 0)
size = file.length();
else
file.setLength(size);
addr = map(file, MAP_RW, 0, size);
this.size = size;
} catch (IOException e) {
file.close();
throw e;
}
}
内容来源于网络,如有侵权,请联系作者删除!