Refine search
FileOutputStream out = new FileOutputStream(filename); [...] out.flush(); out.getFD().sync();
void setFile(final File file) throws IOException { boolean ok = false; final FileOutputStream fos = new FileOutputStream(file, true); try { final OutputStream bos = new BufferedOutputStream(fos); try { this.fileDescriptor = fos.getFD(); this.outputStream = bos; this.file = file; ok = true; } finally { if (! ok) { safeClose(bos); } } } finally { if (! ok) { safeClose(fos); } } }
/** * @deprecated This is not safe. */ public void truncate() throws IOException { try { FileOutputStream fos = new FileOutputStream(mBaseName); fos.getFD().sync(); fos.close(); } catch (FileNotFoundException e) { throw new IOException("Couldn't append " + mBaseName); } catch (IOException e) { } }
FileOutputStream stream = new FileOutputStream(path); FileDescriptor fd = stream.getFD(); fdToStream.put(fd, stream);
@Override public void run() { // Do not write the file if committedOffset has never been updated. if (committedOffset.get() == DEFAULT_COMMITTED_OFFSET) { return; } try (final FileOutputStream fos = new FileOutputStream(committedReadOffsetFile)) { fos.write(String.valueOf(committedOffset.get()).getBytes(StandardCharsets.UTF_8)); // flush stream fos.flush(); // actually sync to disk fos.getFD().sync(); } catch (SyncFailedException e) { LOG.error("Cannot sync " + committedReadOffsetFile.getAbsolutePath() + " to disk. Continuing anyway," + " but there is no guarantee that the file has been written.", e); } catch (IOException e) { LOG.error("Cannot write " + committedReadOffsetFile.getAbsolutePath() + " to disk.", e); } } }
private FileDescriptor openFile(String path) throws FileNotFoundException, IOException { File file = new File(path); FileOutputStream fos = new FileOutputStream(file); // remember th 'fos' reference somewhere for later closing it fos.write((new Date() + " Beginning of process...").getBytes()); return fos.getFD(); }
/** To store an object in a quick & dirty way. */ public static void writeObject(File file, Object object) throws IOException { FileOutputStream fileOut = new FileOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fileOut)); try { out.writeObject(object); out.flush(); // Force sync fileOut.getFD().sync(); } finally { IoUtils.safeClose(out); } }
public static void main (String args[]) throws FileNotFoundException, IOException { Shuffler s = new Shuffler(args[0]) ; FileInputStream f = new FileInputStream ("from") ; FileOutputStream t = new FileOutputStream ("to") ; s.shuffle (f.getFD(), t.getFD()) ; f.close() ; t.close() ; } }
/** To store an object in a quick & dirty way. */ public static void writeObject(File file, Object object) throws IOException { FileOutputStream fileOut = new FileOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fileOut)); try { out.writeObject(object); out.flush(); // Force sync fileOut.getFD().sync(); } finally { IoUtils.safeClose(out); } }
@Test public void testFstatClosedFd() throws Exception { FileOutputStream fos = new FileOutputStream( new File(TEST_DIR, "testfstat2")); fos.close(); try { NativeIO.Stat stat = NativeIO.fstat(fos.getFD()); } catch (NativeIOException nioe) { LOG.info("Got expected exception", nioe); assertEquals(Errno.EBADF, nioe.getErrno()); } }
@Override public void run() { try { fileToEdit.getParentFile().mkdirs(); FileOutputStream fos=new FileOutputStream(fileToEdit); Writer w=new BufferedWriter(new OutputStreamWriter(fos)); try { w.write(text); w.flush(); fos.getFD().sync(); } finally { w.close(); } } catch (IOException e) { Log.e(getClass().getSimpleName(), "Exception writing file", e); } } }
private static BlockOutput setup(LogHeader header, File file) throws IOException { truncate(file, header.getDataEnd()); FileOutputStream fileOutputStream = new FileOutputStream(file, true); FileDescriptor fd = fileOutputStream.getFD(); OutputStream stream = new BufferedOutputStream(fileOutputStream, 1024 * 1024); return header.getCompressionType().createBlockOutput(fd, stream, header.getCompressionBlockSize(), header.getMaxEntriesPerBlock()); }
@Override public void run() { try { fileToEdit.getParentFile().mkdirs(); FileOutputStream fos=new FileOutputStream(fileToEdit); Writer w=new BufferedWriter(new OutputStreamWriter(fos)); try { w.write(text); w.flush(); fos.getFD().sync(); } finally { w.close(); } } catch (IOException e) { Log.e(getClass().getSimpleName(), "Exception writing file", e); } } }
private static BlockOutput setup(LogHeader header, File file) throws IOException { truncate(file, header.getDataEnd()); FileOutputStream fileOutputStream = new FileOutputStream(file, true); FileDescriptor fd = fileOutputStream.getFD(); OutputStream stream = new BufferedOutputStream(fileOutputStream, 1024 * 1024); return header.getCompressionType().createBlockOutput(fd, stream, header.getCompressionBlockSize(), header.getMaxEntriesPerBlock()); }