A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/java_io_fileoutputstream.htm below:

Java - FileOutputStream Class

Java - FileOutputStream Class Introduction

The Java.io.FileOutputStream class is an output stream for writing data to a File or to a FileDescriptor. Following are the important points about FileOutputStream −

Class declaration

Following is the declaration for Java.io.FileOutputStream class −

public class FileOutputStream
   extends OutputStream
Class constructors Sr.No. Constructor & Description 1

FileOutputStream(File file)

This creates a file output stream to write to the file represented by the specified File object.

2

FileOutputStream(File file, boolean append)

This creates a file output stream to write to the file represented by the specified File object.

3

FileOutputStream(FileDescriptor fdObj)

This creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

4

FileOutputStream(String name)

This creates an output file stream to write to the file with the specified name.

5

FileOutputStream(String name, boolean append)

This creates an output file stream to write to the file with the specified name.

Class methods Sr.No. Method & Description 1 void close()

This method closes this file output stream and releases any system resources associated with this stream.

2 protected void finalize()

This method cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.

3 FileChannel getChannel()

This method returns the unique FileChannel object associated with this file output stream.

4 FileDescriptor getFD()

This method returns the file descriptor associated with this stream.

5 void write(byte[] b)

This method writes b.length bytes from the specified byte array to this file output stream.

6 void write(byte[] b, int off, int len)

This method writes len bytes from the specified byte array starting at offset off to this file output stream.

7 void write(int b)

This method writes the specified byte to this file output stream.

Methods inherited

This class inherits methods from the following classes −

Example - Closing FileOutputStream After Writing

The following example shows the usage of Java FileOutputStream close() method. This example demonstrates how to manually close a FileOutputStream after writing data to a file.

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      FileOutputStream fos = null;
      try {
         // Open a file output stream
         fos = new FileOutputStream("output.txt");

         // Write some data to the file
         String data = "Hello, World!";
         fos.write(data.getBytes());

         System.out.println("Data written successfully.");

      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // Close the FileOutputStream in the finally block to ensure closure
         try {
            if (fos != null) {
               fos.close();
               System.out.println("FileOutputStream closed successfully.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result−

Data written successfully.
FileOutputStream closed successfully.
Explanation Example - Using finalize() to Close a FileOutputStream

The following example shows the usage of Java FileOutputStream finalize() method.

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   static class CustomFileOutputStream extends FileOutputStream {
      public CustomFileOutputStream(String fileName) throws IOException {
         super(fileName);
      }

      @Override
      protected void finalize() throws Throwable {
         System.out.println("Finalize method called. Closing FileOutputStream.");
         this.close(); // Explicitly closing the file output stream
         super.finalize();
      }
   }

   public static void main(String[] args) throws IOException {
      CustomFileOutputStream fos = new CustomFileOutputStream("output.txt");
      fos.write("Hello, World!".getBytes());

      // Making object eligible for garbage collection
      fos = null;

      // Requesting garbage collection
      System.gc();

      System.out.println("Garbage Collection Requested.");
   }
}
Output

Let us compile and run the above program, this will produce the following result−

Garbage Collection Requested.
Finalize method called. Closing FileOutputStream.
Explanation Example - Writing Data to a File Using FileChannel

The following example shows the usage of Java FileOutputStream getChannel() method.

FileOutputStreamDemo.java
package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Get FileChannel from FileOutputStream
         FileChannel fileChannel = fos.getChannel();

         // Create a ByteBuffer with data
         ByteBuffer buffer = ByteBuffer.allocate(64);
         buffer.put("Hello, FileChannel!".getBytes());

         // Flip the buffer to prepare for writing
         buffer.flip();

         // Write buffer contents to the file
         fileChannel.write(buffer);

         System.out.println("Data written to file using FileChannel.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result−

Data written to file using FileChannel.
Explanation

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4