FileOutputStream close() in Java

close(): This method is available in the java.io.FileOutputStream  class of Java.

Syntax:

void java.io.FileOutputStream.close() throws IOException

This method closes this file output stream and releases any system resources associated with this stream. This file output stream may no longer be used for writing bytes.

Note:

1. If this stream has an associated channel then the channel is closed as well.

Parameters: NA

Returns: NA

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        FileOutputStream fileOutputStream =
new FileOutputStream(file);

        fileOutputStream.close();

        System.out.println("Successfully closed");
    }
}

Output:

Successfully closed


No comments:

Post a Comment