Java OutputStream Class

Java OutputStream Class

OutStream Class

In the Article, we will Discuss about the OutStream class of java.
In Java, The OutputStream class is an abstract class that serves as the superclass for all classes representing an output stream of bytes.

Java OutStream Class:

The OutputStream class in Java is an abstract class that serves as the superclass for all classes representing an output stream of bytes. It provides a common set of methods for writing data to different types of output destinations, such as files, network sockets, and other streams.

Some of the important methods of the OutputStream class are:

Initializing a FileOutStream object:

The syntax to initialize a FileOutputStream object in Java using the FileOutputStream class constructor is as follows:

Syntax

FileOutputStream fos = new FileOutputStream(String fileName);

In this syntax:

  • fos: is the object reference to the FileOutputStream object that is being created.
  • The String fileName parameter specifies the name of the file that the output stream should be associated with.
Run
import java.io.*;

public class FileOutputStreamExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("output.txt");
            // write data to the file using fos.write() method
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

In this example, we create a new FileOutputStream object to write to the file named output.txt. We can then use the write() method of the FileOutputStream class to write data to the file. Finally, we call the close() method to close the output stream when we are done writing data.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription