PREVIOUS
Buffered Byte Stream:
·
It is based on the byte oriented stream
· The buffered byte stream
classes are buffered I/p
and o/p stream.
BufferedInputStream
· Buffered inputstream class
allows you to “wrap” any inputstream into a buffered stream.this class
implements the fileinputstream class.
· To read the data from file
using the Bufferedrinputstream.
BufferedOutputStream
· Buffered output stream is
similar to any outputstream.
· Bufferedoutput stream is
used to write the data to file.
· The data are written into a
internal buffer, otherwise the buffer reaches its capacity , the buffer output
stream is closed or the buffer outputstream is explicitly flushed.
Default buffer size : 512
bytes
Methods
write() - Writes the
specified byte to the buffered output stream.
flush() - Flushes the
buffered output stream.
// Bufferedinputstream & Bufferedoutputstream
import java.io.*;
class FileIOdemo
{
public
static void main(String args[]) throws IOException
{
int
ch,fsize;
File f=new
File(args[0]);
//FileOutputStream f=new FileOutputStream(f,true)
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(f,true));
System.out.println("Enter a text : ");
do
{
ch=System.in.read();
bos.write(ch);
}while((char)ch != '*');
bos.flush();
bos.close();
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
fsize=bis.available();
System.out.println("\n Contents of file : " + f.getName() +
"\n");
while( (ch = bis.read()) != -1 )
{
System.out.print((char)ch);
}
bis.close();
}
}
Run: java fileiodemo kani.txt
Hai eni
*
contents of file : kani.txt
hai eni
Filereader:
The filereader class creates a reader that you can
use to read the contents of a file.
Filewriter:
Filewriter creates a writer that you can use to
write to a file.
/* FileReader & FileWriter */
import java.io.*;
class fwr
{
public static void main(String ar[])throws
IOException
{
int c;
File f=new File(ar[0]);
FileWriter fw=new FileWriter(f,true);
System.out.println("enter the data");
do
{
c=System.in.read();
fw.write(c);
}
while(c!='$');
fw.flush();
FileReader fr=new FileReader(f);
while( (c=fr.read())!=-1)
{
System.out.print( (char)c);
}
fr.close();
}
}
run: java ll kani
read only:
import java.io.*;
class fread
{
public static void main(String s[])throws Exception
{
int c;
//File f=new File(“fwr.java”);
//FileReader r=new FileReader(f);
File f=new File(s[0]);
FileReader re;
if(f.exists())
{
re =
new FileReader(f);
System.out.println(f.getName());
System.out.println(f.length());
while( (c=re.read())!=-1)
System.out.print( (char)c);
re.close();
}
else
System.out.println("File not Found");
}
}
run: fread fwr.java
getchars() à to extract a string
string
s=”anu”
Sy4ntax:str-var-name.getChars(int start,int end,char[ ] where,int target-start);
s.getchars(1,2,a,5);
import java.io.*;
class fwrite1
{
public static void main(String st[])throws
Exception
{
int i;
FileWriter wr=new FileWriter(st[0],true);
String s = "Welcome this is File Writer
Program";
char a[] = new char[50];
s.getChars(0,s.length(),a,0);
for( i=0;i<a.length;i++)
wr.write(a[i]);
wr.close();
FileReader re=new FileReader(st[0]);
while( (i=re.read())!=-1)
System.out.print( (char)i);
re.close();
}
}
run: java fwrite1 anu
welcome
type anu
welcom
No comments:
Post a Comment