List of usage examples for java.io RandomAccessFile RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
From source file:com.adaptris.fs.NioWorkerTest.java
@Test public void testLockWhileReading() throws Exception { FsWorker worker = createWorker();//from w w w.j a v a 2 s .com File f = File.createTempFile(this.getClass().getSimpleName(), ""); f.delete(); try { worker.put(BYTES, f); RandomAccessFile raf = new RandomAccessFile(f, "rwd"); FileLock lock = raf.getChannel().lock(); try { worker.get(f); fail(); } catch (FsException expected) { assertEquals(OverlappingFileLockException.class, expected.getCause().getClass()); } lock.release(); raf.close(); worker.get(f); } finally { FileUtils.deleteQuietly(f); } }
From source file:hoot.services.info.ErrorLog.java
public String getErrorlog(long maxLength) throws Exception { File file = new File(_errLogPath); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); int lines = 0; StringBuilder builder = new StringBuilder(); long length = file.length(); //length--;/*from w ww. ja v a2s.c o m*/ long startOffset = 0; if (length > maxLength) { startOffset = length - maxLength; } for (long seek = startOffset; seek < length; seek++) { randomAccessFile.seek(seek); char c = (char) randomAccessFile.read(); builder.append(c); } randomAccessFile.close(); return builder.toString(); }
From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java
public static boolean initJPNWNDic() { if (jpnwnURIConceptMap != null && 0 < jpnwnURIConceptMap.size()) { return true; }/*w w w.ja v a 2 s. c om*/ jpnwnURIConceptMap = new HashMap<String, Concept>(); jpnwnWordIDSetMap = new HashMap<String, Set<String>>(); // String baseDir = DODDLEConstants.JPWN_HOME + File.separator; try { // jpnwnTreeDataFile = new RandomAccessFile(baseDir + // TREE_DATA_FILE, "r"); jpnwnTreeDataFile = new RandomAccessFile(Utils.getJPWNFile(TREE_DATA_FILE), "r"); // jpnwnWordDataFile = new RandomAccessFile(baseDir + // WORD_DATA_FILE, "r"); jpnwnWordDataFile = new RandomAccessFile(Utils.getJPWNFile(WORD_DATA_FILE), "r"); // jpnwnConceptDataFile = new RandomAccessFile(baseDir + // CONCEPT_DATA_FILE, "r"); jpnwnConceptDataFile = new RandomAccessFile(Utils.getJPWNFile(CONCEPT_DATA_FILE), "r"); // jpnwnTreeIndexFile = new RandomAccessFile(baseDir + // TREE_INDEX_FILE, "r"); jpnwnTreeIndexFile = new RandomAccessFile(Utils.getJPWNFile(TREE_INDEX_FILE), "r"); // jpnwnWordIndexFile = new RandomAccessFile(baseDir + // WORD_INDEX_FILE, "r"); jpnwnWordIndexFile = new RandomAccessFile(Utils.getJPWNFile(WORD_INDEX_FILE), "r"); // jpnwnConceptIndexFile = new RandomAccessFile(baseDir + // CONCEPT_INDEX_FILE, "r"); jpnwnConceptIndexFile = new RandomAccessFile(Utils.getJPWNFile(CONCEPT_INDEX_FILE), "r"); } catch (IOException ioe) { ioe.printStackTrace(); return false; } return true; }
From source file:com.btoddb.fastpersitentqueue.JournalFileTest.java
@Test public void testInitForWritingThenClose() throws IOException { JournalFile jf1 = new JournalFile(theFile); jf1.initForWriting(new UUID()); assertThat(jf1.isWriteMode(), is(true)); assertThat(jf1.isOpen(), is(true));/*from w w w.ja v a 2 s. c o m*/ assertThat(jf1.getFilePosition(), is((long) JournalFile.HEADER_SIZE)); jf1.close(); assertThat(jf1.isOpen(), is(false)); RandomAccessFile raFile = new RandomAccessFile(theFile, "rw"); assertThat(raFile.readInt(), is(JournalFile.VERSION)); assertThat(Utils.readUuidFromFile(raFile), is(jf1.getId())); assertThat(raFile.readLong(), is(0L)); raFile.close(); }
From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java
private static byte[] getTailBytes(File logFile) throws FileNotFoundException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = null; int len;/* ww w . j av a 2s . c om*/ int size = 1024; try { RandomAccessFile randomAccessFile = new RandomAccessFile(logFile, "r"); long lengthFile = randomAccessFile.length(); if (lengthFile >= LOG_TAIL_FOR_PACKING_SIZE) { randomAccessFile.seek(lengthFile - LOG_TAIL_FOR_PACKING_SIZE); } buf = new byte[size]; while ((len = randomAccessFile.read(buf, 0, size)) != -1) { bos.write(buf, 0, len); } buf = bos.toByteArray(); } catch (IOException e) { log.error("Unable to get tail for log file " + logFile.getName(), e); } finally { IOUtils.closeQuietly(bos); } return buf; }
From source file:au.org.ala.spatial.util.RecordsSmall.java
public RecordsSmall(String dir) throws IOException { this.filename = dir + File.separator; //look for a small file File smallFile = new File(filename + "records.csv.small.species"); if (!smallFile.exists() && new File(filename + "records.csv").exists()) { try {/* w w w.ja v a2 s . c om*/ makeSmallFile(filename); makeUniquePoints(); } catch (Exception e) { logger.error("failed to make small records files", e); } } //read species if (smallFile.exists()) { try { //open points and pointsToSpecies points = new RandomAccessFile(filename + "records.csv.small.points", "r"); pointsToSpecies = new RandomAccessFile(filename + "records.csv.small.pointsToSpecies", "r"); maxPoints = new File(filename + "records.csv.small.pointsToSpecies").length() / 4; pointsDis = new DataInputStream( new BufferedInputStream(new FileInputStream(filename + "records.csv.small.points"))); pointsToSpeciesDis = new DataInputStream(new BufferedInputStream( new FileInputStream(filename + "records.csv.small.pointsToSpecies"))); lsids = FileUtils.readLines(new File(filename + "records.csv.small.species")); getUniquePointsAll(); } catch (Exception e) { logger.error("failed to open small records file", e); } } }
From source file:org.chimi.s4s.fileservice.FileLockBasedThumbnailCreator.java
@Override public File create(FileData fileData, Size newSize) throws IOException { String thumbnailPath = getThumbnailPath(fileData.getFileId(), newSize); File thumbnailFile = new File(thumbnailPath); if (thumbnailFile.exists()) { // ? ?? //from www. j a va2 s .c o m return thumbnailFile; } File lockFile = getLockFile(thumbnailPath); FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel(); FileLock lock = null; try { lock = channel.tryLock(); } catch (OverlappingFileLockException ex) { } if (lock == null) { // ?? , 1 ?? ?? ? ? return createTemporaryThumbnail(thumbnailPath, fileData, newSize); } try { createThumbnail(thumbnailPath, fileData, newSize); return thumbnailFile; } finally { lock.release(); channel.close(); // TODO ? ? } }
From source file:de.micromata.genome.logging.spi.ifiles.IndexedReader.java
public IndexedReader(IndexFileLoggingImpl logger, File logFile, File indexFile) throws IOException { this.logger = logger; this.logFile = logFile; this.indexFile = indexFile; idxRandomAccessFile = new RandomAccessFile(indexFile, "r"); indexChannel = idxRandomAccessFile.getChannel(); long chsize = indexChannel.size(); indexByteBuffer = indexChannel.map(FileChannel.MapMode.READ_ONLY, 0, chsize); indexHeader = IndexHeader.openIndexHeader(indexByteBuffer, indexChannel.size()); }
From source file:fr.letroll.ttorrentandroid.client.storage.FileStorage.java
public FileStorage(File file, long offset, long size) throws IOException { this.target = file; this.offset = offset; this.size = size; File partial = new File(this.target.getAbsolutePath() + TorrentByteStorage.PARTIAL_FILE_NAME_SUFFIX); if (partial.exists()) { logger.debug("Partial download found at {}. Continuing...", partial.getAbsolutePath()); this.current = partial; } else if (!this.target.exists()) { logger.debug("Downloading new file to {}...", partial.getAbsolutePath()); this.current = partial; } else {/* w w w . j a v a 2s .co m*/ logger.debug("Using existing file {}.", this.target.getAbsolutePath()); this.current = this.target; } this.raf = new RandomAccessFile(this.current, "rw"); // Set the file length to the appropriate size, eventually truncating // or extending the file if it already exists with a different size. this.raf.setLength(this.size); this.channel = raf.getChannel(); logger.info("Initialized byte storage file at {} ({}+{} byte(s)).", new Object[] { this.current.getAbsolutePath(), this.offset, this.size, }); }
From source file:com.tps.ttorrent.client.storage.FileStorage.java
public FileStorage(File file, long offset, long size) throws IOException { this.target = file; this.offset = offset; this.size = size; this.partial = new File(this.target.getAbsolutePath() + TorrentByteStorage.PARTIAL_FILE_NAME_SUFFIX); if (this.partial.exists()) { logger.debug("Partial download found at {}. Continuing...", this.partial.getAbsolutePath()); this.current = this.partial; } else if (!this.target.exists()) { logger.debug("Downloading new file to {}...", this.partial.getAbsolutePath()); this.current = this.partial; } else {//from ww w. j av a 2s . c o m logger.debug("Using existing file {}.", this.target.getAbsolutePath()); this.current = this.target; } this.raf = new RandomAccessFile(this.current, "rw"); this.current.deleteOnExit(); // Set the file length to the appropriate size, eventually truncating // or extending the file if it already exists with a different size. this.raf.setLength(this.size); this.channel = raf.getChannel(); logger.info("Initialized byte storage file at {} " + "({}+{} byte(s)).", new Object[] { this.current.getAbsolutePath(), this.offset, this.size, }); }