IO Package in Java

IO package in Java

In this article we will be discussing about IO package in Java.

Various built-in and pre-defined packages are available in java which helps a programmer in various aspects. These built in packages contains various classes and functionalities.

 

IO package in java

Packages in Java

Java contains numerous amount of libraries in which various method and classes are stored. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
And in this tutorial, we will be going to discuss about the lang package in java.

IO Package in Java

Classes for the input and output systems via data streams, serialization, and the file system are provided by the Java.io package. This guide will walk you through the straightforward and useful methods included in the java.io package.

We can categorize the classes available in java.io are:

  • Input Streams.
  • Output Streams.
  • Error Streams.

java.io Input Streams

It is used to read data from the user and is an abstract superclass of the java.io package. Alternatively, we can say that it helps in reading data from files. Using the new keyword, we can construct an object of the input stream class.

Syntax:

InputStream variable_name = new FileInputStream("Textfile_name");

java.io Output Streams

java.io output streams writes the data to an output resource and is a superclass of the java.io package and putting the data into the files. By using the new keyword, we can construct an object of the output stream class.

Syntax:

FileOutputStream variable_name = new FileOutputStream("Textfile_name");

java.io Error Streams

We can say Output streams and error streams are somehow identical. Some Compilers display errors in various aspects. The console receives the same output as output streams.
We can take output of the errors with the help of try and catch block.

Syntax:

try{
    // Block of code
}
catch excepted_error(Exception e){

    // This will print the Exception on the screen
    System.out.println(e);
}

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