Example usage for java.io FileOutputStream getFD

List of usage examples for java.io FileOutputStream getFD

Introduction

In this page you can find the example usage for java.io FileOutputStream getFD.

Prototype

public final FileDescriptor getFD() throws IOException 

Source Link

Document

Returns the file descriptor associated with this stream.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileOutputStream os = new FileOutputStream("outfilename");
    FileDescriptor fd = os.getFD();
    os.write(1);/*from w w w. j  a va  2s.co  m*/
    os.flush();
    fd.sync();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileOutputStream os = new FileOutputStream("outfilename");
    FileDescriptor fd = os.getFD();
    os.write(1);//from  www.j  av  a2  s. c  o m
    os.flush();
    fd.valid();
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    // create new file output stream
    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // get file descriptor instance
    FileDescriptor fd = fos.getFD();

    // test if the file is valid
    boolean bool = fd.valid();

    // print/* w  w w . ja v  a 2  s.c  om*/
    System.out.print("Is file valid? " + bool);

}

From source file:Main.java

/**
 * Perform an fsync on the given FileOutputStream. The stream at this point
 * must be flushed but not yet closed.//from  ww  w .  j a  va  2  s.c o  m
 */
public static boolean sync(FileOutputStream stream) {
    try {
        if (stream != null) {
            stream.getFD().sync();
        }
        return true;
    } catch (IOException e) {
    }
    return false;
}

From source file:Main.java

/**
 * Creates a copy of the file, ensuring the file is written to the disk
 * @param in Source file//from w  w w.  j  a  v  a 2s . co m
 * @param out Destination file
 * @throws IOException if the operation fails
 */
public static void copyFileSync(File in, File out) throws IOException {
    FileInputStream inStream = new FileInputStream(in);
    FileOutputStream outStream = new FileOutputStream(out);
    try {
        copyStream(inStream, outStream);
    } finally {
        inStream.close();
        outStream.flush();
        outStream.getFD().sync();
        outStream.close();
    }
}

From source file:org.eclipse.kura.net.admin.visitor.linux.util.KuranetConfig.java

public static void storeProperties(Properties props) throws IOException, KuraException {
    Properties oldProperties = KuranetConfig.getProperties();

    if (oldProperties == null || !oldProperties.equals(props)) {
        FileOutputStream fos = null;
        try {/*from   ww w  .j ava2 s  .co  m*/
            fos = new FileOutputStream(KURANET_TMP_FILENAME);
            props.store(fos, null);
            fos.flush();
            fos.getFD().sync();

            // move the file if we made it this far
            File tmpFile = new File(KURANET_TMP_FILENAME);
            File file = new File(KURANET_FILENAME);
            if (!FileUtils.contentEquals(tmpFile, file)) {
                if (tmpFile.renameTo(file)) {
                    s_logger.trace("Successfully wrote kuranet props file");
                } else {
                    s_logger.error("Failed to write kuranet props file");
                    throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR,
                            "error while building up new configuration file for kuranet props");
                }
            } else {
                s_logger.info("Not rewriting kuranet props file because it is the same");
            }
        } finally {
            fos.close();
        }
    }
}

From source file:Main.java

/**
 * Copy data from a source stream to destFile. Return true if succeed,
 * return false if failed.// ww  w  .j  ava 2 s. c om
 */
public static boolean copyToFile(InputStream inputStream, File destFile) {
    try {
        if (destFile.exists()) {
            destFile.delete();
        }
        FileOutputStream out = new FileOutputStream(destFile);
        try {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }
        } finally {
            out.flush();
            try {
                out.getFD().sync();
            } catch (IOException e) {
            }
            out.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

public static boolean copyFile(File from, File to, byte[] buf) {
    if (buf == null)
        buf = new byte[BUFFER_SIZE];

    ///*from  w w  w  .ja va  2s.c om*/
    // System.out.println("Copy file ("+from+","+to+")");
    FileInputStream from_s = null;
    FileOutputStream to_s = null;

    try {
        from_s = new FileInputStream(from);
        to_s = new FileOutputStream(to);

        for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s.read(buf))
            to_s.write(buf, 0, bytesRead);

        from_s.close();
        from_s = null;

        to_s.getFD().sync(); // RESOLVE: sync or no sync?
        to_s.close();
        to_s = null;
    } catch (IOException ioe) {
        return false;
    } finally {
        if (from_s != null) {
            try {
                from_s.close();
            } catch (IOException ioe) {
            }
        }
        if (to_s != null) {
            try {
                to_s.close();
            } catch (IOException ioe) {
            }
        }
    }

    return true;
}

From source file:net.timewalker.ffmq4.storage.data.impl.BlockBasedDataStoreTools.java

private static void initAllocationTable(File atFile, int blockCount, int blockSize, boolean forceSync)
        throws DataStoreException {
    log.debug("Creating allocation table (size=" + blockCount + ") ...");

    // Create the file
    try {//from  w  w  w.  java 2s.  c  o  m
        FileOutputStream outFile = new FileOutputStream(atFile);
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outFile));

        out.writeInt(blockCount); // Block count
        out.writeInt(blockSize); // Block size
        out.writeInt(-1); // First block index
        for (int n = 0; n < blockCount; n++)
            out.write(EMPTY_BLOCK);
        out.flush();
        if (forceSync)
            outFile.getFD().sync();

        out.close();
    } catch (IOException e) {
        throw new DataStoreException("Cannot initialize allocation table " + atFile.getAbsolutePath(), e);
    }
}

From source file:org.protocoderrunner.utils.FileIO.java

/**
 * Write the data to the file indicate by fileName. The file is created if
 * it doesn't exist./*from  ww w  .j a v  a 2 s .c o  m*/
 */
public static void write(Activity activity, String data, String fileName) throws IOException {
    FileOutputStream fo = activity.openFileOutput(fileName, 0);
    BufferedWriter bf = new BufferedWriter(new FileWriter(fo.getFD()));
    bf.write(data);
    bf.flush();
    bf.close();
}