Character Streams:
FileReader
and FileWriter class
These classes are used to read and write character files
respectively.
The java platform stores characters using Unicode conventions,
Character stream I/O automatically translates this internal format to and from
the local character set.
If at any point of time internationalization is required for
your application and you had used character stream class , you application can
adapt it without putting much efforts.
However java character streams are used to perform input and
output for 16 bit Unicode.
FileWriter:
This class is used to
write the data to a destination file
package
filehandling_characterstream;
/**
*
* @author shashank
*/
import java.io.*;
public class
FileHandling_CharacterStream {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FileWriter fw = null;
try
{
fw = new
FileWriter("D://Articles//filehandling//filewritecharcters.txt");
String s = "i am using character stream
to write the data";
char [] arr = s.toCharArray();
fw.write(arr);
/*fw.write(s); we can
directly pass string as well */
fw.close();
System.out.println("Success");
}catch(Exception
e){System.out.println(e);}
}
}
FileReader:
This class is used to Read
the data from a source file
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package
filehandling_characterstream;
/**
*
* @author shashank
*/
import java.io.*;
public class
FileHandling_CharacterStream {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FileReader fr = null;
int i=0;
try
{
fr = new FileReader("D://Articles//filehandling//filewritecharcters.txt");
while((i=fr.read())!=-1)
{
System.out.print((char)i);
}
fr.close();
}catch(Exception e){System.out.println(e);}
}
}
Write a
program to copy data from one file to another file
/*
* To change this license
header, choose License Headers in Project Properties.
* To change this template
file, choose Tools | Templates
* and open the template
in the editor.
*/
package filehandling_characterstream;
/**
*
* @author shashank
*/
import java.io.*;
public class FileHandling_CharacterStream {
/**
* @param args the
command line arguments
*/
public static void
main(String[] args) {
// TODO code
application logic here
FileReader fr =
null;
FileWriter fw =
null;
int i=0;
try
{
fr = new
FileReader("D://Articles//filehandling//filewritecharcters.txt");
fw = new
FileWriter("D://Articles//filehandling//newfile.txt");
while((i=fr.read())!=-1)
{
fw.write((char)i);
}
fr.close();
fw.close();
System.out.print("sucess");
}catch(Exception
e){System.out.println(e);}
}
}
No comments:
Post a Comment