Professional Documents
Culture Documents
BASICS
• Not given much importance to console I/O
• Because most real world applications of Java
are not text based console programs
• Graphical oriented interaction
Overview of I/O Streams
• Java programs perform I/O through streams
• To bring in information, a program opens a stream on an
information source (a file, memory, a socket) and reads the
information sequentially, as shown here:
• import java.io.*;
• Java defines two types of streams: byte and character
import java.io.*;
class ReadString
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter the strings");
System.out.println("Enter stop to quit");
do
{
str=br.readLine();
System.out.println(str);
}while(!str.equals("stop"));
}
catch(IOException e)
{
System.out.println("Exeption "+e);
}
}
}
OUTPUT
E:\MERIN\javaprograms>java ReadString
Enter the strings
Enter stop to quit
good
good
morning
morning
welcome
welcome
stop
stop
READ STRINGS FROM CONSOLE
import java.io.*;
class ReadString1
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str[]=new String[100];
System.out.println("Enter the strings");
System.out.println("Enter stop to quit");
for(int i=0;i<100;i++)
{
str[i]=br.readLine();
if(str[i].equals("stop"))
break;
}
System.out.println("The entered strings are :");
for(int i=0;i<100;i++)
{
System.out.println(str[i]);
if(str[i].equals("stop"))
break;
}
}
catch(IOException e)
{
System.out.println("Exeption "+e);
}}}
OUTPUT
E:\MERIN\javaprograms>java ReadString1
Enter the strings
Enter stop to quit
hello
good
morning
stop
The entered strings are :
hello
good
morning
stop
Writing Console output
• System.out is used for writing to console
• For real world programs, the recommended method of writing
to the console is through a PrintWriter stream
• Character based stream class
• Constructor used here:
PrintWriter(OutputStream out,boolean flushonNewline)
• out is an object of type OutputStream, flushonNewline controls
whether Java flushes the outputstream everytime a println()
method is called
• If it is true, flushing takes place automatically
• It reads a single byte from the file and returns the byte as an
integer value.
• It returns -1 when end of the file is encountered
w
o
r
l
d
COPY ONE FILE TO ANOTHER
import java.io.*;
class CopyFile
{
public static void main(String args[])
{
int i;
FileInputStream fin;
FileOutputStream fout;
try
{
try
{
fin=new FileInputStream(args[0]);
}
catch(FileNotFoundException e)
{ System.out.println("File Not Found");
return;
}
try
{
fout=new FileOutputStream(args[1]);
}
catch(FileNotFoundException e)
{ System.out.println("File Not Found");
return;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Index");
return;
}
try
{ do
{
i=fin.read();
if(i!=-1)
{
System.out.print((char) i);
fout.write(i);
}
}while(i!=-1);
fin.close();
fout.close();
}
catch(IOException e)
{
System.out.println("IOException"); return;
}
}
}
OUTPUT
D:\merin>java CopyFile hai.txt out.txt
Good Morning!!!