0% found this document useful (0 votes)
61 views6 pages

Java IO: Input-Output in Java

This document discusses input and output streams in Java. It describes the three standard streams - System.in for input, System.out for normal output, and System.err for errors. It explains input and output streams can read from or write to arrays, files, and devices. Common input streams include FileInputStream and ByteArrayInputStream, while common output streams are FileOutputStream and BufferedOutputStream. It provides examples of reading from and writing to files using FileReader and FileWriter classes.

Uploaded by

Aditya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
61 views6 pages

Java IO: Input-Output in Java

This document discusses input and output streams in Java. It describes the three standard streams - System.in for input, System.out for normal output, and System.err for errors. It explains input and output streams can read from or write to arrays, files, and devices. Common input streams include FileInputStream and ByteArrayInputStream, while common output streams are FileOutputStream and BufferedOutputStream. It provides examples of reading from and writing to files using FileReader and FileWriter classes.

Uploaded by

Aditya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Java IO : Input-output in Java

Java brings various Streams with its I/O package that helps the user to
perform all the input-output operations. These streams support all the types
of objects, data-types, characters, files etc to fully execute the I/O
operations.

Before exploring various input and output streams lets look at 3 standard or
default streams that Java has to provide which are also most common in
use:
1. System.in: This is the standard input stream that is used to read
characters from the keyboard or any other standard input device.
2. System.out: This is the standard output stream that is used to
produce the result of a program on an output device like the
computer screen.
3. System.err :- This is the standard output stream this at is used to
produce the error message of a program on an output device like
the computer screen.

Types of Streams:
Depending on the type of operations, streams can be divided into two
primary classes:
1. Input Stream:  These streams are used to read data that must be
taken as an input from a source array or file or any peripheral
device. For eg., FileInputStream, BufferedInputStream,
ByteArrayInputStream etc.
Output Stream:  These streams are used to write data as outputs into an
array or file or any output peripheral device. For eg., FileOutputStream,
BufferedOutputStream, ByteArrayOutputStream etc.

Demo :- ByteArrayInputStream :- ByteArrayDemo

Parameters of read() – InputStream


Buffer :- is an object of byte array type.
Offset :- start position
Length:- the length of contents in the buffer

read(byte[] buffer, int offset, int length)


Exceptions :- IOException , FileNotFound, NullPointerException
import java.io.*;
//Java program demonstrating FileInputStream
class ReadFile
{
public static void main(String args[]) throws IOException, NullPointerException
{

//create a pointer to the file using FileInputStream


FileInputStream fin= new FileInputStream("f:\\Streams.txt");

// returning the total no. of bytes from the Inputstream to be read


System.out.println("Number of remaining bytes:"+ fin.available());

fin.skip(4); // skip the number of bytes from being read

System.out.println("FileContents :");
//read characters from FileInputStream and write them
int ch;
while((ch=fin.read())!=-1)
System.out.print((char)ch);
//close the file
fin.close();
}
}
FileOutputStream in Java
FileOutputStream is an outputstream for writing data/streams of raw bytes to file or
storing data to file. FileOutputStream is a subclass of OutputStream. To write primitive
values into a file, we use FileOutputStream class. For writing byte-oriented and
character-oriented data, we can use FileOutputStream but for writing character-oriented
data, FileWriter is more preferred.

// java program to use FileOutputStream object for writing


// data

import java.io.*;

class FileExample {
public static void main(String[] args)
throws IOException
{
int i;

// create a fileoutputstream object


FileOutputStream fout = new FileOutputStream(filename,true);

// we need to transfer this string to files


String st = "This is my new file content";

char ch[] = st.toCharArray();


for (i = 0; i < st.length(); i++) {

// we will write the string by writing each


// character one by one to file
fout.write(ch[i]);
}

// by doing fout.flush() all the changes which have


// been made till now in RAM had been now saved to
// hard disk
fout.flush();
//to close all the objects
fout.close();
}
}
File handling in Java using FileWriter and FileReader
Java FileWriter and FileReader classes are used to write and read data from
text files (they are Character Stream classes). It is recommended not to use
the FileInputStream and FileOutputStream classes if you have to read and
write any textual information as these are Byte stream classes.

FileWriter is useful to create a file writing characters into it.


 This class inherits from the OutputStream class.
 The constructors of this class assume that the default character
encoding and the default byte-buffer size are acceptable. To specify
these values yourself, construct an OutputStreamWriter on a
FileOutputStream.
 FileWriter is meant for writing streams of characters. For writing
streams of raw bytes, consider using a FileOutputStream.
 FileWriter creates the output file , if it is not present already.

// Creating a text File using FileWriter


import java.io.FileWriter;
import java.io.IOException;
class CreateFile
{
public static void main(String[] args) throws IOException
{
// Accept a string
String str = "File Handling in Java using "+
" FileWriter and FileReader";

// attach a file to FileWriter


FileWriter fw=new FileWriter("filename");

// read character wise from string and write


// into FileWriter
for (int i = 0; i < str.length(); i++)
fw.write(str.charAt(i));

System.out.println("Writing successful");
//close the file
fw.flush();
fw.close();
}
}

FileReader
FileReader is useful to read data in the form of characters from a ‘text’ file.
 This class inherit from the InputStreamReader Class.
 The constructors of this class assume that the default character
encoding and the default byte-buffer size are appropriate. To
specify these values yourself, construct an InputStreamReader on a
FileInputStream.
 FileReader is meant for reading streams of characters. For reading
streams of raw bytes, consider using a FileInputStream.
// Reading data from a file using FileReader
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadFile
{
public static void main(String[] args) throws IOException
{
// variable declaration
int ch;

// check if File exists or not


FileReader fr=null;
try
{
fr = new FileReader("filename.txt");
}
catch (FileNotFoundException fe)
{
System.out.println("File not found");
}

// read from FileReader till the end of file


while ((ch=fr.read())!=-1)
System.out.print((char)ch);

// close the file


fr.close();
}
}

Assignments
1.
System.in
System.out
Print
Println
Printf()
System.err
2.
Buffer
3.
File

You might also like