ByteStream :
Byte Streams allows a programmer to work with the binary data in a file.In this streams data are accessed as a sequence of bytes all types other than the text or character that are deal in this stream.Byte Streams contains a different types of byte stream classes.
There are mainly two parts :
- Input Stream
- Output Stream
InputStream
- The Input stream class defines the functionality that is available for all byte input streams
- The method provide by the InputStream class are :
int read()
int readbyte(byte[] buffer)
int read(byte[] buffer,int offset, int length)
OutputStream
- The Ouput Stream class defines the functionality that is available for all byte output streams
- The method provided by OutputStream class are
int write()
int writebyte(byte[] buffer)
int write(byte[] buffer,int offset, int length)
Other method includes :
void close()
void flush()
Example :
package com.tech;
import java.io.*;
public class writeData {
public writeData() throws InterruptedException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\\WriteData1.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try
{
for(int i=1;i<=10;i++)
{
fos.write(i);
}
fos.flush();
fos.close();
System.out.println("successfully write");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package com.tech;
import java.io.FileInputStream;
public class readData {
public readData() {
try{
FileInputStream fis =new FileInputStream("D:\\WriteData1.txt");
int i=0;
while((i=fis.read())!=-1)
{
System.out.println(i);
}
fis.close();
System.out.println("successfully read");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package com.tech;
public class Demo {
public static void main(String args[]) throws Exception
{
writeData wd=new writeData();
readData rd=new readData();
}
}
Output :
successfully write
1
2
3
4
5
6
7
8
9
10
successfully read
0 Comment(s)