public class Test8 {
/**
* @param args
*/
public static void test1(){
FileReader fis=null;
FileWriter fos =null;
try {
fis = new FileReader("C:\\Users\\TZXXB\\Desktop\\123\\abc.txt");
fos = new FileWriter("C:\\Users\\TZXXB\\Desktop\\123\\def.txt");
int length=fis.read();
while(length!=-1){
fos.write(length);
length=fis.read();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void test2(){
FileReader fis=null;
FileWriter fos =null;
try {
fis = new FileReader("C:\\Users\\TZXXB\\Desktop\\123\\abc.txt");
fos = new FileWriter("C:\\Users\\TZXXB\\Desktop\\123\\def.txt");
char[] a = new char[1024];
int length=fis.read(a);
while(length!=-1){
fos.write(a);
String s = new String(a,0,length);
System.out.println(s);
length=fis.read(a);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void test3(){
FileReader fis=null;
FileWriter fos =null;
try {
fis = new FileReader("C:\\Users\\TZXXB\\Desktop\\123\\abc.txt");
fos = new FileWriter("C:\\Users\\TZXXB\\Desktop\\123\\def.txt");
char[] a = new char[100];
int length=fis.read(a);
while(length!=-1){
fos.write(a,0,length);
String s = new String(a,0,length);
System.out.println(s);
length=fis.read(a);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
test2();
test3();
}
}
Java filereader 与 filewriter
最新推荐文章于 2024-06-09 18:21:26 发布