SlideShare a Scribd company logo
Streams in Java
Input Stream
Output
Stream
Input Streams are streams which receive or read data
Output Streams are streams which send or write data
Always
keep in
mind
A stream carries data just as a water pipe carries water from
one place to another.
Another classification of streams is ‘byte streams’ & ‘text stream’.
Byte Stream->
• Data in the form of individual bytes.
• It can be used for any text,images,audio and video.
Examples::
FileInputStream
FileOutputStream
BufferedInputStream
Buffered0utputStream
Text Stream->
• Data as characters of each 2 bytes.
• It can be used only for text file.
Examples::
FileReader
FileWriter
BufferedReader
BufferedWriter
Note:- Stream is useful to move data from one place to another place, without Stream
it is not possible to move data in java.
Stream Example- Creating a file using FileOutputStream
Here we will write some text into Mytext.txt
• For writing the text from keyboard we can take any class such as
DataInputStream,BufferedReader,Scanner class,etc.
• FileOutputStream class will be used for writing that text into
Mytext.txt
import java.io.*;
public class Createfile
{
public static void main(String[] args)throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println(“Enter any String”);
String value=dis.readLine();
System.out.println("String which you have entered is "+value);
}
}
System.in is
representing
keyboard & it is
attached into
DataInputStream
Output of
previous
code
import java.io.*;
public class Createfile
{
public static void main(String[] args)throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
FileOutputStream fout=new FileOutputStream("Mytext.txt");
System.out.println("Enter text (@ at the end):");
char ch;
while((ch=(char)dis.read())!='@')
{
fout.write(ch);
}
fout.close();
}
}
Myfile.txt file is attached into FileOutputStream object
In following example ,we can see how to write
text/string into a file.
This write method is used for
writing into file
Compile the class
Run the class
Writing the contents into file
Opening the file
Stream Example- Reading a file using FileInputStream
Here we will read String from Mytext.txt file-
• FileInputStream class will be used for reading string from Mytext.txt
import java.io.*;
class Readfile{
public static void main(String args[]) throws IOException
{
FileInputStream fin=new FileInputStream("Mytext.txt");
System.out.println("File contents are:: ");
int ch;
while((ch=fin.read())!=-1){
System.out.print((char)ch);
}
}
}
Myfile.txt file is attached into FileInputStream object
-1 indicate there is no content in
the file
…fin object has method read()
which will retrive content but
return information in integer
format
Converting integer information
into character.
Output
To be continue…………
Thanks For Watching…
-Java Foundation Technology

More Related Content

What's hot

15. text files
15. text files15. text files
15. text files
Konstantin Potemichev
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
JayasankarPR2
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
primeteacher32
 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
32sql server
32sql server32sql server
32sql server
Sireesh K
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
Sunil Patel
 
30csharp
30csharp30csharp
30csharp
Sireesh K
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
Basic of java
Basic of javaBasic of java
Basic of java
Kavitha713564
 
Java I/O
Java I/OJava I/O
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
Files in java
Files in javaFiles in java
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 

What's hot (20)

15. text files
15. text files15. text files
15. text files
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
32sql server
32sql server32sql server
32sql server
 
Java IO
Java IOJava IO
Java IO
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
30csharp
30csharp30csharp
30csharp
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
 
Basic of java
Basic of javaBasic of java
Basic of java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Files in java
Files in javaFiles in java
Files in java
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 

Similar to Stream

Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
cs19club
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
GayathriRHICETCSESTA
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
phanleson
 
Io Streams
Io StreamsIo Streams
Io Streams
phanleson
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
File Input and Output in Java Programing language
File Input and Output in Java Programing languageFile Input and Output in Java Programing language
File Input and Output in Java Programing language
BurhanKhan774154
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Io streams
Io streamsIo streams
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course
 
15 Text files
15 Text files15 Text files
15 Text files
maznabili
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ssuser9d7049
 
Java sockets
Java socketsJava sockets
Java sockets
Stephen Pradeep
 

Similar to Stream (20)

Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Io Streams
Io StreamsIo Streams
Io Streams
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
File Input and Output in Java Programing language
File Input and Output in Java Programing languageFile Input and Output in Java Programing language
File Input and Output in Java Programing language
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Io streams
Io streamsIo streams
Io streams
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
15 Text files
15 Text files15 Text files
15 Text files
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
Java sockets
Java socketsJava sockets
Java sockets
 

Stream

  • 1. Streams in Java Input Stream Output Stream
  • 2. Input Streams are streams which receive or read data Output Streams are streams which send or write data Always keep in mind A stream carries data just as a water pipe carries water from one place to another.
  • 3. Another classification of streams is ‘byte streams’ & ‘text stream’. Byte Stream-> • Data in the form of individual bytes. • It can be used for any text,images,audio and video. Examples:: FileInputStream FileOutputStream BufferedInputStream Buffered0utputStream Text Stream-> • Data as characters of each 2 bytes. • It can be used only for text file. Examples:: FileReader FileWriter BufferedReader BufferedWriter Note:- Stream is useful to move data from one place to another place, without Stream it is not possible to move data in java.
  • 4. Stream Example- Creating a file using FileOutputStream Here we will write some text into Mytext.txt • For writing the text from keyboard we can take any class such as DataInputStream,BufferedReader,Scanner class,etc. • FileOutputStream class will be used for writing that text into Mytext.txt
  • 5. import java.io.*; public class Createfile { public static void main(String[] args)throws IOException { DataInputStream dis=new DataInputStream(System.in); System.out.println(“Enter any String”); String value=dis.readLine(); System.out.println("String which you have entered is "+value); } } System.in is representing keyboard & it is attached into DataInputStream
  • 7. import java.io.*; public class Createfile { public static void main(String[] args)throws IOException { DataInputStream dis=new DataInputStream(System.in); FileOutputStream fout=new FileOutputStream("Mytext.txt"); System.out.println("Enter text (@ at the end):"); char ch; while((ch=(char)dis.read())!='@') { fout.write(ch); } fout.close(); } } Myfile.txt file is attached into FileOutputStream object In following example ,we can see how to write text/string into a file. This write method is used for writing into file
  • 8. Compile the class Run the class Writing the contents into file Opening the file
  • 9. Stream Example- Reading a file using FileInputStream Here we will read String from Mytext.txt file- • FileInputStream class will be used for reading string from Mytext.txt
  • 10. import java.io.*; class Readfile{ public static void main(String args[]) throws IOException { FileInputStream fin=new FileInputStream("Mytext.txt"); System.out.println("File contents are:: "); int ch; while((ch=fin.read())!=-1){ System.out.print((char)ch); } } } Myfile.txt file is attached into FileInputStream object -1 indicate there is no content in the file …fin object has method read() which will retrive content but return information in integer format Converting integer information into character.
  • 13. Thanks For Watching… -Java Foundation Technology