------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
代码如下,根据毕向东老师的代码改编
/*
* 将一个盘中的文件复制到另一个盘中
*/
import java.io.*;
public class CopyText {
public static void main(String[] args) throws IOException{
//调用方法2
Copy_2();
//调用方法1
//Copy_1();
}
public static void Copy_1(){
//创建引用
FileWriter fw=null;
FileReader fr=null;
try{
//创建目的地
fw=new FileWriter("G:\\DateDemo_copy.text");
//与已有文件关联
fr=new FileReader("D:\\Eclipse\\java\\IO\\src\\itcast\\black\\DateDemo.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{
//判断对象fw是否为空,不为空则关闭对象
if(fw!=null)
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
try{
//判断对象fr是否为空,不为空则关闭对象
if(fr!=null)
fr.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
//从D盘中读一个字符,就往G盘中写一个字符
public static void Copy_2(){
//创建引用
FileWriter fw=null;
FileReader fr=null;
try{
//创建目的地
fw=new FileWriter("G:\\CalenderDemo_copy.text");
//与已有文件关联
fr=new FileReader("D:\\Eclipse\\java\\IO\\src\\itcast\\black\\CalendarDemo.java");
int ch=0;
while((ch=fr.read())!=-1){
fw.write(ch);
}
}
catch(IOException e){
System.out.println(e.toString());
}
//关闭对象
finally{
try{
if(fw!=null)
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
try{
if(fr!=null)
fr.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>