java.io.RandomAccessFile.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中java.io.RandomAccessFile.<init>方法的一些代码示例,展示了RandomAccessFile.<init>的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RandomAccessFile.<init>方法的具体详情如下:
包路径:java.io.RandomAccessFile
类名称:RandomAccessFile
方法名:<init>

RandomAccessFile.<init>介绍

[英]Constructs a new RandomAccessFile based on file and opens it according to the access string in mode.

mode may have one of following values:

"r"The file is opened in read-only mode. An IOException is thrown if any of the write methods is called."rw"The file is opened for reading and writing. If the file does not exist, it will be created."rws"The file is opened for reading and writing. Every change of the file's content or metadata must be written synchronously to the target device."rwd"The file is opened for reading and writing. Every change of the file's content must be written synchronously to the target device.
[中]基于文件构造一个新的RandomAccessFile,并在模式下根据访问字符串打开它。
mode may have one of following values:
“r”文件以只读模式打开。如果调用了任何写入方法,则会引发IOException。“rw”打开文件进行读写。如果文件不存在,将创建该文件。“rws”打开文件进行读写。文件内容或元数据的每次更改都必须同步写入目标设备。“rwd”打开文件进行读写。文件内容的每次更改都必须同步写入目标设备。

代码示例

代码示例来源: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: libgdx/libgdx

public FileWriter (String name) throws FileNotFoundException {
  this.file = new RandomAccessFile(new File(name), "rw");
}

代码示例来源: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/nifi

private RandomAccessFile initializeFile(String path, String data) throws IOException {
  File file = new File(path);
  if(file.exists()) {
    file.delete();
  }
  assertTrue(file.createNewFile());
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
  randomAccessFile.write(data.getBytes());
  randomAccessFile.close();
  return randomAccessFile;
}

代码示例来源:origin: apache/flink

@SuppressWarnings("resource")
private void createSpillingChannel() throws IOException {
  currentSpillFile = new File(tempDir, spillFilePrefix + (fileCounter++) + ".buffer");
  currentChannel = new RandomAccessFile(currentSpillFile, "rw").getChannel();
}

代码示例来源:origin: SonarSource/sonarqube

public AllProcessesCommands(File directory) {
 if (!directory.isDirectory() || !directory.exists()) {
  throw new IllegalArgumentException("Not a valid directory: " + directory);
 }
 try {
  sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
  mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MAX_SHARED_MEMORY);
 } catch (IOException e) {
  throw new IllegalArgumentException("Unable to create shared memory : ", e);
 }
}

代码示例来源: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: stackoverflow.com

public static void aMethod(){
  RandomAccessFile f = new RandomAccessFile(new File("whereDidIPutTHatFile"), "rw");
  long aPositionWhereIWantToGo = 99;
  f.seek(aPositionWhereIWantToGo); // this basically reads n bytes in the file
  f.write("Im in teh fil, writn bites".getBytes());
  f.close();
}

代码示例来源:origin: apache/flume

/**
 * Attempts to acquire an exclusive lock on the directory.
 *
 * @return A lock object representing the newly-acquired lock or
 * <code>null</code> if directory is already locked.
 * @throws IOException if locking fails.
 */
@SuppressWarnings("resource")
private FileLock tryLock(File dir) throws IOException {
 File lockF = new File(dir, FILE_LOCK);
 lockF.deleteOnExit();
 RandomAccessFile file = new RandomAccessFile(lockF, "rws");
 FileLock res = null;
 try {
  res = file.getChannel().tryLock();
 } catch (OverlappingFileLockException oe) {
  file.close();
  return null;
 } catch (IOException e) {
  LOGGER.error("Cannot create lock on " + lockF, e);
  file.close();
  throw e;
 }
 return res;
}

代码示例来源: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: stackoverflow.com

import java.io.*;

public class IOUtil {

  public static byte[] readFile(String file) throws IOException {
    return readFile(new File(file));
  }

  public static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
      // Get and check length
      long longlength = f.length();
      int length = (int) longlength;
      if (length != longlength)
        throw new IOException("File size >= 2 GB");
      // Read file and return data
      byte[] data = new byte[length];
      f.readFully(data);
      return data;
    } finally {
      f.close();
    }
  }
}

代码示例来源:origin: alibaba/jstorm

/**
 * 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 {
  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();
  long val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
  raf.close();
  return val;
}

代码示例来源:origin: cmusphinx/sphinx4

/** Lock the test suite so we can manipulate the set of tests */
private void lock() throws IOException {
  RandomAccessFile raf = new RandomAccessFile(lockFile, "rw");
  lock = raf.getChannel().lock();
  raf.close();
}

代码示例来源: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: 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: google/guava

private static MappedByteBuffer mapInternal(File file, MapMode mode, long size)
  throws IOException {
 checkNotNull(file);
 checkNotNull(mode);
 Closer closer = Closer.create();
 try {
  RandomAccessFile raf =
    closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
  FileChannel channel = closer.register(raf.getChannel());
  return channel.map(mode, 0, size == -1 ? channel.size() : size);
 } catch (Throwable e) {
  throw closer.rethrow(e);
 } finally {
  closer.close();
 }
}

代码示例来源:origin: neo4j/neo4j

public static void truncateFile( File file, long position ) throws IOException
{
  try ( RandomAccessFile access = new RandomAccessFile( file, "rw" ) )
  {
    truncateFile( access.getChannel(), position );
  }
}

代码示例来源:origin: bumptech/glide

raf = new RandomAccessFile(file, "r");
 channel = raf.getChannel();
 return channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength).load();
} finally {
 if (channel != null) {
   raf.close();
  } catch (IOException e) {

代码示例来源:origin: redisson/redisson

public static byte[] readBytes(File file, int fixedLength) throws IOException {
  if (!file.exists()) {
    throw new FileNotFoundException(MSG_NOT_FOUND + file);
  }
  if (!file.isFile()) {
    throw new IOException(MSG_NOT_A_FILE + file);
  }
  long len = file.length();
  if (len >= Integer.MAX_VALUE) {
    throw new IOException("File is larger then max array size");
  }
  if (fixedLength > -1 && fixedLength < len) {
    len = fixedLength;
  }
  byte[] bytes = new byte[(int) len];
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  randomAccessFile.readFully(bytes);
  randomAccessFile.close();
  return bytes;
}

代码示例来源:origin: apache/flink

public void open(File outputFile) throws IOException {
  outputFile.mkdirs();
  if (outputFile.exists()) {
    outputFile.delete();
  }
  outputFile.createNewFile();
  outputRAF = new RandomAccessFile(outputFile, "rw");
  outputRAF.setLength(mappedFileSizeBytes);
  outputRAF.seek(mappedFileSizeBytes - 1);
  outputRAF.writeByte(0);
  outputRAF.seek(0);
  outputChannel = outputRAF.getChannel();
  fileBuffer = outputChannel.map(FileChannel.MapMode.READ_WRITE, 0, mappedFileSizeBytes);
}

相关文章