Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Java Input Output

Rating
-
Sold
-
Pages
113
Uploaded on
05-03-2023
Written in
2021/2022

Java I/O (Input and Output) is used to process the input and produce the output. Java uses the concept of a stream to make I/O operation fast. The package contains all the classes required for input and output operations. We can perform file handling in Java by Java I/O API.

Show more Read less
Institution
Course

Content preview

Java I/O

Java I/O (Input and Output) is used to process the input and produce the output.

Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the
classes required for input and output operations.

We can perform file handling in Java by Java I/O API.

Stream

A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it
is like a stream of water that continues to flow.

In Java, 3 streams are created for us automatically. All these streams are attached with the console.

 System.out: standard output stream

 System.in: standard input stream

 System.err: standard error stream


Code to print output and an error message to the console.

 System.out.println("simple message");

 System.err.println("error message");


Code to get input from console.

int i=System.in.read();//returns ASCII code of 1st character

System.out.println((char)i);//will print the character

OutputStream vs InputStream

OutputStream

 Java application uses an output stream to write data to a destination; it may be a file, an
array, peripheral device or socket.

InputStream

 Java application uses an input stream to read data from a source; it may be a file, an array,
peripheral device or socket.

Working of Java OutputStream and InputStream by the figure given below.




1

,OutputStream class

OutputStream class is an abstract class. It is the superclass of all classes representing an output
stream of bytes. An output stream accepts output bytes and sends them to some sink.

Useful methods of OutputStream


Method Description


1) public void write(int)throws IOException is used to write a byte to the current output stream.


2) public void write(byte[])throws is used to write an array of byte to the current output
IOException stream.


3) public void flush()throws IOException flushes the current output stream.


4) public void close()throws IOException is used to close the current output stream.




OutputStream Hierarchy




2

,InputStream class

InputStream class is an abstract class. It is the superclass of all classes representing an input stream
of bytes.

Useful methods of InputStream


Method Description


public abstract int read()throws reads the next byte of data from the input stream. It returns -1
IOException at the end of the file.


public int available()throws returns an estimate of the number of bytes that can be read
IOException from the current input stream.


public void close()throws is used to close the current input stream.
IOException




InputStream Hierarchy




Java FileOutputStream Class

Java FileOutputStream is an output stream used for writing data to a file.

If you have to write primitive values into a file, use FileOutputStream class. You can write byte-
oriented as well as character-oriented data through FileOutputStream class. But, for character-
oriented data, it is preferred to use FileWriter than FileOutputStream.

FileOutputStream class declaration

Declaration for Java.io.FileOutputStream class:

3

, public class FileOutputStream extends OutputStream

FileOutputStream class methods


Method Description


protected void finalize() It is used to clean up the connection with the file output stream.


void write(byte[] ary) It is used to write ary.length bytes from the byte array to the file
output stream.


void write(byte[] ary, int off, It is used to write len bytes from the byte array starting at offset off to
int len) the file output stream.


void write(int b) It is used to write the specified byte to the file output stream.


FileChannel getChannel() It is used to return the file channel object associated with the file
output stream.


FileDescriptor getFD() It is used to return the file descriptor associated with the stream.


void close() It is used to closes the file output stream.




Java FileOutputStream

Example 1: write byte

import java.io.FileOutputStream;

public class FileOutputStreamExample {

public static void main(String args[]){

try{

FileOutputStream fout=new FileOutputStream("D:\\testout.txt");

fout.write(65);

fout.close();

System.out.println("success...");



4

Written for

Course

Document information

Uploaded on
March 5, 2023
Number of pages
113
Written in
2021/2022
Type
OTHER
Person
Unknown

Subjects

$8.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
adityas1

Get to know the seller

Seller avatar
adityas1 Bytes Softech Delhi
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions