请问一下复制文本以下两种方法,方法一的代码简单,方法二运用了流的缓冲区,哪种方法更好?
方法一
import java.io.*;
class One
{
public static void main(String[] args)
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("000.txt");
fr = new FileReader("Demo.java");
char[] buf = new char[1024];
int len = 0;
while ((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if (fw!=null)
fw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
try
{
if (fr!=null)
fr.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
}
}
}
方法二
import java.io.*;
class Two
{
public static void main(String[] args)
{
BufferedWriter bufw = null;
BufferedReader bufr = null;
try
{
bufw = new BufferedWriter(new FileWriter("000.txt"));
bufr = new BufferedReader(new FileReader("Demo.java"));
String line = null;
while ((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if (bufw!=null)
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
try
{
if (bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
}
}
}
|