0% found this document useful (0 votes)
18 views

Streams

Streams are used to read and write data. There are two types of streams: input and output streams. Input streams read data from sources while output streams write data to destinations. Byte streams handle bytes of data and character streams handle characters. FileInputStream and FileOutputStream are used to read from and write to files respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Streams

Streams are used to read and write data. There are two types of streams: input and output streams. Input streams read data from sources while output streams write data to destinations. Byte streams handle bytes of data and character streams handle characters. FileInputStream and FileOutputStream are used to read from and write to files respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Streams:

Streams are the sequence of bits(data).


There are two types of streams:
Input Streams
Output Streams

Input Streams: Input streams are used to read the data from various input devices
like keyboard, file, network, etc.

Output Streams: Output streams are used to write the data to various output
devices like monitor, file, network, etc.

There are two types of streams based on data:

Byte Stream: used to read or write byte data.


Character Stream: used to read or write character data.

Byte Input Stream:

This is used to read byte data from various input devices.


InputStream is an abstract class and it is the super
class of all the input byte streams.

List of Byte Input Streams:[io package]


-BufferedInputStream
-FileInputStream
-DataInputStream
-FilterInputStream
-ByteArrayInputStream
-ObjectInputStream

Byte Output Stream:


This is used to write byte data to various output devices.
OutputStream is an abstract class and it is the superclass for all the output
byte streams.
List of Byte Output Streams:
-BufferedOutputStream
-FileOutputStream
-ByteArrayOutputStream
-ObjectOutputStream
-DataOutputStream

Character Input Stream:


This is used to read char data from various input devices.
Reader is an abstract class and is the super class for
all the character input streams.

List of Character Input Streams:


- BufferedReader
- InputStreamReader
- StringReader
- PipedReader
- CharArrayReader
- FilterReader
Character Output Stream:
This is used to write char data to various output devices.
Writer is an abstract class and is the super class of all the character output
streams.
List of Character Output Stream:
-BufferedWriter
-PrintWriter
-FilterWriter
-PipedWriter
-OutputStreamWriter

FileInputStream
------------------
It is predefined class to read data from file.
read() is predefined method to read data.
read() method throws an IOException[Checked exception].
Incase of end of file, read() returns -1.
notepad demo.txt
demo.txt
This is file program

ReadContent.java
import java.io.*;
class ReadContent
{
public static void main(String args[])
{

try
{
FileInputStream f=new FileInputStream("demo.txt");
int d=f.read();
System.out.print((char)d);
d=f.read();
System.out.print((char)d);
d=f.read();
System.out.print((char)d);
f.close();
}
catch(IOException e)
{System.out.println(e);}
}
}

program to read contents of file using loop.


---------------------------------------------
import java.io.*;
class ReadData
{
public static void main(String args[])
{
try
{
FileInputStream f=new FileInputStream("demo.txt");
int d;
while((d=f.read())!=-1)
{
System.out.print((char)d);
}
f.close();
}
catch(IOException e)
{System.out.println(e);}
}
}

program to print contents letterwise


-----------------------------------------
import java.io.*;
class RData
{
public static void main(String args[])
{
try
{
FileInputStream f=new FileInputStream("data.txt");
int d;
while((d=f.read())!=-1)
{
System.out.print((char)d);
Thread.sleep(200);
}
f.close();
}
catch(Exception e)
{System.out.println(e);}
}
}

notepad stud.txt
----------------------
Roll Name Branch
1 Ram CSE
2 Gita IT
3 Ramesh CST
4 Suresh MECH
program to read stud.txt file
-----------------------------
import java.io.*;
class ReadStud
{
public static void main(String args[])
{
try
{
FileInputStream f=new FileInputStream("stud.txt");
int d;
while((d=f.read())!=-1)
{
System.out.print((char)d);
}
f.close();
}
catch(IOException e)
{System.out.println(e);}
}
}

program to read contents of javaprogram


-------------------------------------------
import java.io.*;
class ReadJava
{
public static void main(String args[])
{
try
{
FileInputStream f=new FileInputStream("ReadJava.java");
int d;
while((d=f.read())!=-1)
{
System.out.print((char)d);
}
f.close();
}
catch(IOException e)
{System.out.println(e);}
}
}
program to print content of the file in half second interval
---------------------------------------------------------------
import java.io.*;
import java.util.*;
class ReadUF
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter file name to read:");
String fname=sc.next();
try
{
FileInputStream f=new FileInputStream(fname);
int d;
while((d=f.read())!=-1)
{

System.out.print((char)d);
Thread.sleep(200);
}
f.close();
}
catch(Exception e)
{System.out.println(e);}
}
}

program to print content according to user input


---------------------------------------------------------------
import java.io.*;
import java.util.*;
class ReadUF
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter filename to read:");
String fname=sc.next();
try
{
FileInputStream f=new FileInputStream(fname);
int d;
while((d=f.read())!=-1)
{
System.out.print((char)d);
Thread.sleep(200);
}
f.close();
}
catch(Exception e)
{System.out.println(e);}
}
}

FileOutputStream
-----------------
It is predefined class to create a new file in program directory.
write() is predefined method to write data in to the file.
Constructor is used to create new file.
FileOutputStream f=new FileOutputStream("demo.txt") //create new file
FileOutputStream f=new FileOutputStream("demo.txt",true); //open in append mode

program to create new file in program directory


--------------------------------------------------
import java.io.*;
class NewFile
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("mdata.txt"); //create new file
f.close();
}
catch(IOException e){}
}
}

program to write single character in new file.


----------------------------------------------
import java.io.*;
class WriteFile
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("data.txt"); //create new file
f.write('Z');
f.close();
}
catch(IOException e){}
}
}
program to write ABC in file
===============================
import java.io.*;
class WriteABC
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("abc.txt"); //create new file
f.write('A');
f.write('B');
f.write('C');
f.close();
}
catch(IOException e){}
}
}

program to append X and Y in existing file abc.txt [ABCXY]


--------------------------------------
import java.io.*;
class AppX
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("abc.txt",true); //append data
file
f.write('X');
f.write('Y');
f.close();
}
catch(IOException e){}
}

program to write char,number in file.


------------------------------------
import java.io.*;
class FileN
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("anum.txt");
f.write('X');
f.write(65);
f.write(97);
f.write(49);
f.close();
}
catch(IOException e){}
}
}

program to write A to Z in file


------------------------------------
import java.io.*;
class AtoZ
{
public static void main(String args[])
{
try
{
FileOutputStream f=new FileOutputStream("atoz.txt");
for(int i=65;i<=90;i++)
f.write(i);
f.close();
}
catch(IOException e){}
}

getBytes()
------------
This method is used to convert string to byte array.
It is part of String class.
byte r[]="This is java".getBytes();
import java.io.*;
class PrintB
{
public static void main(String args[])
{
byte r[]="ABCDEF".getBytes();
for(byte val:r)
System.out.print(val+" ");
}
}

You might also like