Streams allow data to be transferred between locations. There are two main types of streams: byte streams which transfer data as a series of bytes, and character streams which transfer data as a series of characters. Byte streams can be used for any type of file, while character streams are only used for text files. Input streams are used for reading data and output streams are used for writing data. Buffered streams improve performance by transferring data in blocks rather than a byte at a time. File input/output streams allow reading from and writing to files on the disk.
1. streams
1.what is streams?
a)streams is a flowing data from one place to another place.but in java
applications readind and writing
data together called iostreams.
2.what is the advantage of streams?
a)streams is used to move a data from one place to another place.advantage of
streams is accept a data from
from input device and send a data to output device.
3.what are the types of IOstreams?
a)IOstreams are classified into two catagory
1.Byte streams.
2.character streams.
Byte Streams:
byte streams are used to transfer a data from location to another location in
form of byte by byte.
(or)
byte streams are used to readind and writing data inform of byte by byte
by using byte streams we can transfer any type of file like
text,vedio,audio....
Character Streams:
character streams are used to read and writting a data in form of character by
character.and each
character take two bytes of memory so spead of transfer a data in character
stream is spead compare
to byte streams.
using character streams we can send a only text files....
4.Byte streams classified into two types?
1.Input Streams
2.OutPut Streams
InputStreams:
this stream is used to perform the reading data from various resources inform of
byte by byte
eg:
1.FileInputStreams
2.DataInputStreams
OutPutStreams:
this stream is used to perform the writing operation into various resources in
form of byte by byte
eg:
1.FileOutPutStreams
2.DataOutPutStreams
5.Character Streams forther classifed into two types?
1.reading
2.writing
reading:
the reading similar to inputstreams to perform a reading operation from various
resources in form of character
by character.
eg:
file reading
writing:
the writing similar to outputstream to perform a writing operation into various
resource in form of character by
character
eg:
file writing
6.DataInputStream?
a)data input stream is one of the class used to reading a data from various
reasouces.
eg:
import java.io.DataInputStream;
import java.io.IOException;
class DataInputStreamDemo{
2. public static void main(String[] ar) throws IOException{
DataInputStream dis=new DataInputStream(System.in);
System.out.print("enter the characte:");
int ch;
while((ch=dis.read())!='1'){
System.out.print((char)ch);
}
}
}
7.ByteArrayInputStream?
a)this class is used to read the the data from byte array
syntax for creation:
ByteArrayInputStream bai=new ByteArrayInputStream(byte b[]);
eg:
import java.io.*;
import java.io.ByteArrayInputStream;
class ByteInputStreamDemo{
public static void main(String[] ar){
String s="rajasekhar";
byte bi[]=s.getBytes();
int ch;
ByteArrayInputStream bs=new ByteArrayInputStream(bi);
for(int i=0;i<1;i++){
while((ch=bs.read())!=-1){
System.out.print((char)ch);
}
bs.reset();
}
}
}
8.what is FileInputStream?
a)this class is used to read the data from the file in form of byte by byte.it
is possible to read data
from text file.
syntax:
FileInputStream fi=new FileInputStream("txt file");
eg:
import java.io.FileInputStream;
import java.io.IOException;
class FileInputStreamDemo{
public static void main(String[] ar) throws IOException{
FileInputStream fs=new FileInputStream("hi.txt");
System.out.println("the file content:");
int ch;
while((ch=fs.read())!=-1){
System.out.print((char)ch);
}
}
}
9.what is BufferedInputStreamReader?
a)this class is used to improve the performance of application by transfering an
application group of bytes
together.
before we saw in inputstream to transfer data in form of byte byte.but we use
bufferedinputstream we transfer group of bytes
at a time .
this class doesn't connect to resources directly and it takes supported of some
other inputstream
syntax:
BufferedInputStream bis=new BufferedInputStream(InputStream);
eg:
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
3. class FileInputStreamDemo{
public static void main(String[] ar) throws IOException{
FileInputStream fs=new FileInputStream("hi.txt");
BufferedInputStream bi=new BufferedInputStream(fs);
System.out.println("the file content:");
int ch;
while((ch=bi.read())!=-1){
System.out.print((char)ch);
}
}
}
10.what is file output stream?
a)this class is used to write the data into resource in form byte by byte.
syntax:
FileOutputStream fi=new FileOutputStream(file);
FileOutputStream fi=new FileOutputStream(string);
the above two syntax is used to write the content into specified file.if
specified file is not found then automatically
file will be created with the specified file.if the spacified file is alredy
available then it will be overide the already
existing with the new content.
eg:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class FileInputStreamDemo{
public static void main(String[] ar) throws IOException{
FileInputStream fs=new FileInputStream("hi.txt");
FileOutputStream fo=new FileOutputStream("one.txt");
//BufferedInputStream bi=new BufferedInputStream(fs);
System.out.println("the file content:");
int ch;
while((ch=fs.read())!=-1){
fo.write(ch);
}
fs.close();
fo.close();
}
}