[go: up one dir, main page]

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

8. Files IO, byte&character stream

java OOP

Uploaded by

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

8. Files IO, byte&character stream

java OOP

Uploaded by

alwk2787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Files and I/O

• Java performs I/O through Streams. A Stream is linked to a physical layer by java I/O system to
make input and output operation in java. In general, a stream means continuous flow of data.
• Streams are clean way to deal with input/output without having every part of your code
understand the physical.
• Java encapsulates Stream under java.io package. Java defines two types of streams. They are,
 Byte Stream: It provides a convenient means for handling input and output of byte.
 Character Stream: It provides a convenient means for handling input and output of characters.
Character stream uses Unicode and therefore can be internationalized.
Byte Stream Classes
 Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and
OutputStream.

 These two abstract classes have several concrete classes that handle various devices such as disk
files, network connection etc.
 Some important Byte stream classes.

 These classes define several key methods. Two most important are
 read() : reads byte of data.
 write() : Writes byte of data.
Character Stream Classes
 Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader
and Writer.

 These two abstract classes have several concrete classes that handle unicode character.
 Some important Character stream classes

 Reading Console Input


We use the object of BufferedReader class to take inputs from the keyboard.

Reading Characters
read() method is used with BufferedReader object to read characters. As this function returns integer
type value has we need to use typecasting to convert it into char type.
int read() throws IOException
Below is a simple example explaining character input.
class CharRead
{
public static void main( String args[])
{
BufferedReader br = new Bufferedreader(new InputstreamReader(System.in));
char c = (char)br.read(); //Reading character
}
}

Reading Strings
To read string we have to use readLine() function with BufferedReader class's object.
String readLine() throws IOException
Program to take String input from Keyboard in Java
import java.io.*;
class MyInput
{
public static void main(String[] args)
{
String text;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
text = br.readLine(); //Reading String
System.out.println(text);
}
}
Program to read from a file using BufferedReader class
import java. Io *;
class ReadTest
{
public static void main(String[] args)
{
try
{
File fl = new File("d:/myfile.txt");
BufferedReader br = new BufferedReader(new FileReader(fl)) ;
String str;
while ((str=br.readLine())!=null)
{
System.out.println(str);
}
br.close();
fl.close();
}
catch (IOException e)
{ e.printStackTrace(); }
}
}
Program to write to a File using FileWriter class
import java. Io *;
class WriteTest
{
public static void main(String[] args)
{
try
{
File fl = new File("d:/myfile.txt");
String str="Write this string to my file";
FileWriter fw = new FileWriter(fl) ;

You might also like