0% found this document useful (0 votes)
1 views4 pages

Java Io Stream Notes

Java programming language

Uploaded by

dharshinit06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Java Io Stream Notes

Java programming language

Uploaded by

dharshinit06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

I/O Stream
Definition
Input/Output (I/O) stream in Java is used to read data from a source and write data to a
destination. Streams represent a sequence of data.
Syntax
InputStream in;
OutputStream out;
Example
Reading data from keyboard and displaying it.
Program
import [Link].*;
public class IOStreamDemo {
public static void main(String args[]) throws Exception {
int a;
[Link]("Enter a character:");
a = [Link]();
[Link]("You entered: " + (char)a);
}
}
Output
Enter a character:
A
You entered: A
2. Concepts of Streams
Definition
A stream is a flow of data between a program and an input or output source. Streams make
it easier to perform input and output operations.
Syntax
InputStream object;
OutputStream object;
Example
Reading input from keyboard using streams.
Program
import [Link].*;
class StreamConceptDemo {
public static void main(String args[]) throws Exception {
int ch;
[Link]("Enter character:");
ch = [Link]();
[Link]("Character entered: " + (char)ch);
}
}
Output
Enter character:
B
Character entered: B
3. Stream Classes
Definition
Stream classes are predefined classes in Java that are used to perform input and output
operations. They are available in [Link] package.
Syntax
FileInputStream fin;
FileOutputStream fout;
Example
Using FileInputStream class.
Program
import [Link].*;
class StreamClassDemo {
public static void main(String args[]) throws Exception {
FileInputStream fin = new FileInputStream("[Link]");
int i;
while((i=[Link]())!=-1) {
[Link]((char)i);
}
[Link]();
}
}
Output
Displays the contents of the file
4. Byte and Character Stream
Definition
Byte streams handle data in the form of bytes, while character streams handle data in the
form of characters.
Syntax
FileInputStream fin;
FileReader fr;
Example
Reading characters using FileReader.
Program
import [Link].*;
class CharStreamDemo {
public static void main(String args[]) throws Exception {
FileReader fr = new FileReader("[Link]");
int i;
while((i=[Link]())!=-1) {
[Link]((char)i);
}
[Link]();
}
}
Output
Displays characters from the file
5. Reading Console Input and Writing Console Output
Definition
Java allows reading input from the console and displaying output on the console using
[Link] and [Link].
Syntax
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
Example
Reading a line from console.
Program
import [Link].*;
class ConsoleIODemo {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter your name:");
String name = [Link]();
[Link]("Hello " + name);
}
}
Output
Enter your name:
Ameira
Hello Ameira
6. File Handling
Definition
File handling in Java is used to create, read, write and delete files using classes available in
the [Link] package.
Syntax
FileWriter fw = new FileWriter("[Link]");
Example
Writing data to a file.
Program
import [Link].*;
class FileHandlingDemo {
public static void main(String args[]) throws Exception {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java");
[Link]();
[Link]("Data written to file");
}
}
Output
Data written to file

You might also like