码迷,mamicode.com
首页 > 其他好文 > 详细

2014/9/21

时间:2014-09-22 00:46:01      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   java   ar   2014   div   

1

public class wori
{
public static void main(String args[])
{
FileWriter out = null;
FileReader in = null;
int b = 0;
int n = 0; //要是放到try里,最后一句无法通过,try也是作用域
try
{
in = new FileReader("d:/1.txt");
out = new FileWriter("d:/2.txt");
while((b = in.read()) != -1)
{
System.out.println(b);
n++;
out.write(b);
}
out.close();
in.close();
}
catch(FileNotFoundException e)
{
System.out.println("File not found");
System.exit(-1);

}
catch(IOException e)
{
System.out.println("File read error;");
System.exit(-1);
}
System.out.println("一共有" + n + "个数"); 
}
}

  


2
BufferedReader BufferedWriter

public class wori
{
public static void main(String args[])
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter("d:/2.txt")); //
BufferedReader in = new BufferedReader(new FileReader("d:/1.txt")); //
String s;
while((s = in.readLine()) != null) //readLine返回null
{
out.write(s);
out.newLine(); //
}
out.close();
in.close();
}
catch(IOException e)
{
System.out.println("File read error");
}
}
}

  

3
缓冲流InputStreamReader和OutputStreamWriter,可以实现一个字符一个字符的读写

public class wori
{
public static void main(String args[])
{
try
{
OutputStreamWriter out 
= new OutputStreamWriter(new FileOutputStream("d:/1.txt")); //
out.write("worinima");    
out.close(); //
out = new OutputStreamWriter(new FileOutputStream("d:/2.txt", true)); //true代表追加,不加true,原来的内容会被清除
out.write("cao");
out.close();
}

catch(IOException e)
{
System.out.println("File read error");
System.exit(-1);
}    
}
}

 

2014/9/21

标签:style   blog   color   io   os   java   ar   2014   div   

原文地址:http://www.cnblogs.com/melon2014/p/3985146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!