- Java.io package classes
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
Java.io.File.sync() Method
Description
The java.io.File.sync() method forces all system buffers to synchronize with the underlying device.
Declaration
Following is the declaration for java.io.File.sync() method −
public void sync()
Parameters
NA
Return Value
The method does not return any value.
Exception
SyncFailedException − This exception is thrown when the buffer cannot be flushed or because the system cannot guarantee synchronization of all the buffers with the physical media.
Example
The following example shows the usage of java.io.File.sync() method.
package com.tutorialspoint; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; public class FileDemo { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; FileDescriptor fd = null; byte[] b = {65,66,67,68,69,70}; try { fos = new FileOutputStream("c:/java test.txt"); fd = fos.getFD(); // writes byte to file output stream fos.write(b); // flush data from the stream into the buffer fos.flush(); // confirms data to be written to the disk fd.sync(); // create input stream fis = new FileInputStream("c:/java test.txt"); int value = 0; // for every available bytes while((value = fis.read())!= -1) { // converts bytes to char char c = (char)value; // prints char System.out.print(c); } // print System.out.print("\nSync() successfully executed!!"); } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases system resources if(fos!=null) fos.close(); if(fis!=null) fis.close(); } } }
Let us compile and run the above program, this will produce the following result −
ABCDEF Sync() successfully executed!!
java_io_filedescriptor.htm
Advertisements