In order to read/ write into or from files java provides basically three ways
- Reading /Writing bytes
- Reading/ Writing Characters
- Reading/ Writing Primitive data
Reading / Writing Bytes: Java provides FileInputStream and FileOutputStream to read/ write bytes from or into the files.
FileInputStream:
It is a byte input stream derived from InputStream used to read from disk file in form of bytes.
Constructors:
FileInputStream (String filename): constructs a FileInputStream object using the file name given as String.
FileInputStream (File filename): constructs a FileInputStream object using the file name given as File object.
Eg:
String file=”Sample.txt”;
FileInputStream fis=new FileInputStream (file);
int ch;
while((ch=fis.read())!=-1)
System.out.print((char)ch);
fis.close();
FileOutputStream:
It is a byte output stream derived from OutputStream used to write data to a disk file in form of bytes.
Constructor:
FileOutputStream (String filename): constructs a FileOutputStream object using the file name given as String.
FileOutputStream (File filename): constructs a FileOutputStream object using the file name given as File object.
Eg:
String file=”Sample.txt”;
FileOutputStream fos=new FileOutputStream (file);
byte b[]=”This is a sample program”.getBytes();
for(int i=0;i<b.length;i++)
fos.write(b[i]);
fos.close();
Note: The FileInputStream and FileOutputStream throw two checked exceptions namely FileNotFoundException and IOException.
Reading / Writing Characters:
Java provides FileReader and FileWriter to read/ write characters from or into the files.
FileReader:
It is a character input stream derived from Reader used to read from disk file in form of characters.
Constructors:
FileReader (String filename): constructs a FileReader object using the file name given as String.
FileReader (File filename): constructs a FileReader object using the file name given as File object.
Eg:
String file=”Sample.txt”;
FileReader fr=new FileReader (file);
int ch;
while((ch=fr.read())!=-1)
System.out.print((char)ch);
fr.close();
FileWriter:
It is a character output stream derived from Writer used to write data to a disk file in form of characters.
Constructors:
FileWriter (String filename): constructs a FileWriter object using the file name given as String.
FileWriter (File filename): constructs a FileWriter object using the file name given as File object.
Eg:
String file=”Sample.txt”;
FileWriter fw=new FileWriter (file);
char c[]=”This is a sample program”.toCharArray();
for(int i=0;i<c.length;i++)
fw.write(c[i]);
fw.close();
Note: The FileReader and FileWriter throw two checked exceptions namely FileNotFoundException and IOException.
Reading / Writing Primitive data types:
Java
provides DataInputStream and DataOutputStream inorder to read or write
primitive data from or into the Files.
DataInputStream:
It is a byte
filter stream which is used to read data in required format. It requires a node
stream to process.
Constructors:
DataInputStream(InputStream is): creates a DataInputStream object for
the specified byte input stream.
DataInputStream provides methods to read required format of the data
from an output stream
Method
|
Description
|
int read(byte[]
b)
|
It is used to
read the number of bytes from the input stream.
|
int read(byte[]
b, int off, int len)
|
It is used to
read len bytes of
data from the input stream.
|
int readInt()
|
It is used to
read input bytes and return an int value.
|
byte readByte()
|
It is used to
read and return the one input byte.
|
char readChar()
|
It is used to
read two input bytes and returns a char value.
|
double
readDouble()
|
It is used to
read eight input bytes and returns a double value.
|
boolean
readBoolean()
|
It is used to
read one input byte and return true if byte is non zero, false if byte is
zero.
|
int
skipBytes(int x)
|
It is used to
skip over x bytes of data from the input stream.
|
String readUTF()
|
It is used to
read a string that has been encoded using the UTF-8 format.
|
void
readFully(byte[] b)
|
It is used to
read bytes from the input stream and store them into the buffer array.
|
void
readFully(byte[] b, int off, int len)
|
It is used to
read len bytes from
the input stream.
|
Eg: String file=”StudentDetails.txt”;
FileInputStream
fis=new FileInputStream (file);
DataInputStream
dis=new DataInputStream(fis);
int rNo=dis.readInt();
String name=dis.readLine();
float marks=dis.readFloat();
dis.close();
fis.close():
DataOutputStream:
It is a byte
filter stream which is used to read data in required format. It requires a node
stream to process.
Constructors:
DataOutputStream(OutputStream is): creates a DataOutputStream object
for the specified byte input stream.
DataOutputStream provides methods to write required format of the data
to an output stream
Method
|
Description
|
int size()
|
It is used to
return the number of bytes written to the data output stream.
|
void write(int
b)
|
It is used to
write the specified byte to the underlying output stream.
|
void write(byte[]
b, int off, int len)
|
It is used to
write len bytes of data to the output stream.
|
void
writeBoolean(boolean v)
|
It is used to
write Boolean to the output stream as a 1-byte value.
|
void
writeChar(int v)
|
It is used to
write char to the output stream as a 2-byte value.
|
void
writeChars(String s)
|
It is used to
write string to the output stream as a sequence of characters.
|
void
writeByte(int v)
|
It is used to
write a byte to the output stream as a 1-byte value.
|
void
writeBytes(String s)
|
It is used to
write string to the output stream as a sequence of bytes.
|
void
writeInt(int v)
|
It is used to
write an int to the output stream
|
void
writeShort(int v)
|
It is used to
write a short to the output stream.
|
void
writeShort(int v)
|
It is used to write
a short to the output stream.
|
Eg:
String file=”StudentDetails.txt”;
FileOutputStream
fos=new FileOutputStream (file);
DataOutputStream
dis=new DataOutputStream(fos);
int rNo =1234;
String name=”ramu”;
float marks=789F;
dos.writeInt(rNo);
dos.writeChars(name);
dos.writeFloat(marks);
dis.close();
fis.close():
No comments:
Post a Comment