本帖最后由 夏天那抹蓝╮ 于 2013-9-6 16:33 编辑
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyDemo {
/**
* 复制文件
*/
public static void main(String[] args) {
Copy();
}
public static void Copy() {
FileWriter fw=null;
FileReader fr=null;
try{
fw=new FileWriter("Luntan.java");
fr=new FileReader("Copy.txt");
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
{
if(fr!=null)
try{
fr.close();
}
catch(IOException e)
{
}
if(fw!=null)
try{
fw.close();
}
catch(IOException e)
{
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyDemo {
/**
* 通过缓冲区复制文件
*/
public static void main(String[] args) {
Copy();
}
public static void Copy() {
BufferedWriter bufw=null;
BufferedReader bufr=null;
try{
bufw=new BufferedWriter(new FileWriter("Luntan.java"));
bufr=new BufferedReader(new FileReader("Copy.txt"));
String line=null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
}
catch(IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
if(bufr!=null)
try{
bufr.close();
}
catch(IOException e)
{
}
if(bufw!=null)
try{
bufw.close();
}
catch(IOException e)
{
}
}
}
}
|