import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileTest {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:\\Test.txt");
fos = new FileOutputStream("D:\\Test(2).txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("复制文件出错");
} finally {
try {
if(fis != null)
fis.close();
} catch (IOException ex) {
throw new RuntimeException("读取流关闭失败");
} finally {
try {
if(fos != null)
fos.close();
} catch (IOException exc) {
new RuntimeException("写入流关闭失败");
}
}
}
}
} |
|