Documente Academic
Documente Profesional
Documente Cultură
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,
1.
Byte Stream : It provides a convenient means for handling input and output of byte.
2.
Character Stream : It provides a convenient means for handling input and output of
characters. Character stream uses Unicode and therefore can be internationalized.
These two abstract classes have several concrete classes that handle various devices such as disk
files, network connection etc.
Description
BufferedInputStream
BufferedOutputStream
DataInputStream
DataOutputStream
An output stream that contain method for writing java standard data type
FileInputStream
FileOutputStream
InputStream
OutputStream
PrintStream
These classes define several key methods. Two most important are
1.
2.
These two abstract classes have several concrete classes that handle unicode character.
Description
BufferedReader
BufferedWriter
FileReader
FileWriter
InputStreamReader
OutputStreamReader
PrintWriter
Reader
Writer
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
Stream
A stream is a sequence of data.In Java a stream is composed of bytes. It's called a stream
because it's like a stream of water that continues to flow.
In java, 3 streams are created for us automatically. All these streams are attached with
console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Let's see the code to print output and error message to the console.
1.
System.out.println("simple message");
2.
System.err.println("error message");
Let's see the code to get input from console.
1.
2.
OutputStream
Java application uses an output stream to write data to a destination, it may be a file,an
array,peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source, it may be a file,an
array,peripheral device or socket.
Let's understand working of Java OutputStream and InputStream by the figure given below.
OutputStream class
OutputStream class is an abstract class.It is the superclass of all classes representing an
output stream of bytes. An output stream accepts output bytes and sends them to some
sink.
Description
is used to write a byte to the current output stream.
IOException:
2) public void write(byte[])throws
IOException:
stream.
InputStream class
InputStream class is an abstract class.It is the superclass of all classes representing an
input stream of bytes.
Description
reads the next byte of data from the input stream.It returns -1 at the
IOException:
end of file.
returns an estimate of the number of bytes that can be read from the
IOException:
IOException:
import java.io.*;
class Test{
public static void main(String args[]){
try{
FileOutputstream fout=new FileOutputStream("abc.txt");
String s="Sachin Tendulkar is my favourite player";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){system.out.println(e);}
}
Output:success...
import java.io.*;
class SimpleRead{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
}
}
Output:Sachin is my favourite player.
Example of Reading the data of current java file and writing it into
another file
We can read the data of any file using the FileInputStream class whether it is java file,
image file, video file etc. In this example, we are reading the data of C.java file and writing
it into another file M.java.
1.
import java.io.*;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
class C{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("C.java");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1){
fout.write((byte)i);
}
fin.close();
}
}
import java.io.*;
class Test{
public static void main(String args[])throws Exception{
FileOutputStream fout=new FileOutputStream("f1.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout);
String s="Sachin is my favourite player";
byte b[]=s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
}
}
Output:
success...
import java.io.*;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
class SimpleRead{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("f1.txt");
BufferedInputStream bin=new BufferedInputStream(fin);
int i;
while((i=bin.read())!=-1){
System.out.println((char)i);
}
bin.close();
fin.close();
}catch(Exception e){system.out.println(e);}
}
}
Output:
Sachin is my favourite player
Description
FileWriter(String file)
FileWriter(File file)
Description
closes FileWriter.
import java.io.*;
class Simple{
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("abc.txt");
fw.write("my name is sachin");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("success");
}
}
Output:
success...
Description
FileReader(String
It gets filename in string. It opens the given file in read mode. If file
file)
FileReader(File file)
It gets filename in file instance. It opens the given file in read mode. If
file doesn't exist, it throws FileNotFoundException.
Description
closes FileReader.
import java.io.*;
class Simple{
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("abc.txt");
int i;
while((i=fr.read())!=-1)
System.out.println((char)i);
fr.close();
}
}
Output:
my name is sachin
Method
Description
it moves the scanner position to the next line and returns the value
as a string.
public double
nextDouble()
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}
Output:
Enter your rollno
111
Enter your name
Ratan
Enter
450000
Rollno:111 name:Ratan fee:450000
import java.util.*;
public class ScannerTest2{
public static void main(String args[]){
String input = "10 tea 20 coffee 30 tea buiscuits";
Scanner s = new Scanner(input).useDelimiter("\\s");
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.nextInt());
9.
10.
11.
System.out.println(s.next());
s.close();
}}
Output:
10
tea
20
coffee
directories.
*/
b = file.mkdirs();
}
if (b)
System.out.println("Directory successfully created");
else
System.out.println("Failed to create directory");
}}