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; } }
public FileWriter (String name) throws FileNotFoundException { this.file = new RandomAccessFile(new File(name), "rw"); }
import java.io.RandomAccessFile; RandomAccessFile f = new RandomAccessFile(fileName, "r"); byte[] b = new byte[(int)f.length()]; f.readFully(b);
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; }
@SuppressWarnings("resource") private void createSpillingChannel() throws IOException { currentSpillFile = new File(tempDir, spillFilePrefix + (fileCounter++) + ".buffer"); currentChannel = new RandomAccessFile(currentSpillFile, "rw").getChannel(); }
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); } }
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);
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(); }
/** * 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; }
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; } }
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(); } } }
/** * 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; }
/** 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(); }
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); }
/** * 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; } }
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(); } }
public static void truncateFile( File file, long position ) throws IOException { try ( RandomAccessFile access = new RandomAccessFile( file, "rw" ) ) { truncateFile( access.getChannel(), position ); } }
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) {
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; }
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); }