将C盘一个文本文件复制到D盘
代码:
import java.io.*;
import javax.management.RuntimeErrorException;
public class code
{
public static void main(String[] args) throws IOException {
copy_2();
}
public static void copy_2() throws IOException {
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter("demo.txt");
fr = new FileReader("demo.java");
char[] ch = new char[1024];
int len = 0;
while((len=fr.read(ch))!=-1) {
fw.write(ch,0,len);
}
} catch (Exception e) {
throw new RuntimeException("读写失败");
}finally {
try {
fr.close();
} catch (Exception e2) {
// TODO: handle exception
}
try {
fw.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//从C盘读一个字符串,就往D盘写一个字符
public static void copy_1() throws IOException {
//创建目的地
FileWriter fw = new FileWriter("demo.txt");
//与已有文件关联
FileReader fr = new FileReader("demo.java");
int ch = 0;
while((ch=fr.read())!=-1) {
fw.write(ch);
}
fw.close();
fr.close();
}
}