The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.io  [35 examples] > Files  [9 examples]

e27. Forcing Updates to a File to the Disk

In some applications, such as transaction processing, it is necessary to ensure that an update has been made to the disk. FileDescriptor.sync() blocks until all changes to a file are written to disk.
    try {
        // Open or create the output file
        FileOutputStream os = new FileOutputStream("outfilename");
        FileDescriptor fd = os.getFD();
    
        // Write some data to the stream
        byte[] data = new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE};
        os.write(data);
    
        // Flush the data from the streams and writers into system buffers.
        // The data may or may not be written to disk.
        os.flush();
    
        // Block until the system buffers have been written to disk.
        // After this method returns, the data is guaranteed to have
        // been written to disk.
        fd.sync();
    } catch (IOException e) {
    }

 Related Examples
e19. Determining If a File or Directory Exists
e20. Creating a File
e21. Getting the Size of a File
e22. Deleting a File
e23. Creating a Temporary File
e24. Renaming a File or Directory
e25. Moving a File or Directory to Another Directory
e26. Getting and Setting the Modification Time of a File or Directory

See also: Directories    Encodings    Filenames and Pathnames    Parsing    Reading and Writing    Serialization   


© 2002 Addison-Wesley.