4
4
4
File
Stream
A stream is a sequence of bytes that has a source(input
stream ) or a destination(output stream).
Standard Stream:
Methods Description
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
// Demonstration of File input stream.
import java.io.*;
class file_output_stream
{
public static void main(String args[])
{
try
{
int i,s;
FileInputStream fin=new FileInputStream("abc.txt");
FileOutputStream fout=new FileOutputStream("a.txt");
try
{
do
{
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
System.out.println("Successfully copied file");
}
catch(IOException e)
{
System.out.println("File Error");
}
}
}
The Character Stream Classes
Character streams are defined by using two class hierarchies. At
the top are two abstract classes, Reader and Writer.
FileReader(String filePath)
FileReader(File fileObj)
// Demonstrate FileReader.
import java.io.*;
class filereaderdemo
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("filereaderdemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
{
System.out.println(s);
}
fr.close();
}
}
FileWriter
}
File Class:
It creates an InputStreamReader
InputStreamReader(InputStream
that uses the given charset
in, CharsetDecoder dec)
decoder.
It creates an OutputStreamWriter
OutputStreamWriter(OutputStream
that uses the default character
out)
encoding.
It writes a single
void write(int c)
character.
write(String str, int off, It writes a portion of a
void
int len) string.
BufferedReader Class