注释处,请大家帮忙看下.最后想问下,下面的catch用哪个Exception比较好?
private static File copyFolder(String oldPath, String newPath){
File newFile = new File(newPath);
try{
newFile.mkdir();
File oldFolder = new File(oldPath);
String[] allFileName = oldFolder.list();
File temp = null;
for(int i = 0; i < allFileName.length; i++){
if(oldPath.endsWith(System.getProperty("file.separator"))){
temp = new File(oldPath + allFileName[i]);
}else{
temp = new File(oldPath + System.getProperty("file.separator") + allFileName[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + System.getProperty("file.separator")
+ (temp.getName()).toString());
BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
BufferedWriter outputWriter = new BufferedWriter(new OutputStreamWriter(output));
char[] data = new char[1024*5]; //1、不知道这个大小合不合适
int len = 0;
while((len = inputReader.read(data)) != -1){
outputWriter.write(data, 0, len);
}
output.flush(); //2、这下全关闭了吗?
output.close();
input.close();
inputReader.close();
//outputWriter.close(); //3、这里必须注释掉才能运行
}
if(temp.isDirectory()){
copyFolder(oldPath + System.getProperty("file.separator") + allFileName[i],
newPath + System.getProperty("file.separator") + allFileName[i]);
}
}
}catch(Exception e){
e.printStackTrace();
System.out.println("复制出现错误!");
}
return newFile;
}
private static File copyFolder(String oldPath, String newPath){
File newFile = new File(newPath);
try{
newFile.mkdir();
File oldFolder = new File(oldPath);
String[] allFileName = oldFolder.list();
File temp = null;
for(int i = 0; i < allFileName.length; i++){
if(oldPath.endsWith(System.getProperty("file.separator"))){
temp = new File(oldPath + allFileName[i]);
}else{
temp = new File(oldPath + System.getProperty("file.separator") + allFileName[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + System.getProperty("file.separator")
+ (temp.getName()).toString());
BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
BufferedWriter outputWriter = new BufferedWriter(new OutputStreamWriter(output));
char[] data = new char[1024*5]; //1、不知道这个大小合不合适
int len = 0;
while((len = inputReader.read(data)) != -1){
outputWriter.write(data, 0, len);
}
output.flush(); //2、这下全关闭了吗?
output.close();
input.close();
inputReader.close();
//outputWriter.close(); //3、这里必须注释掉才能运行
}
if(temp.isDirectory()){
copyFolder(oldPath + System.getProperty("file.separator") + allFileName[i],
newPath + System.getProperty("file.separator") + allFileName[i]);
}
}
}catch(Exception e){
e.printStackTrace();
System.out.println("复制出现错误!");
}
return newFile;
}
解决方案 »
- 挺有意思的问题?
- Runtime.getRuntime().exec和在windows命令窗口里运行程序有什么区别?
- div定位问题(当点击一个图片按纽,图片居中显示)
- swing是个设计经典的GUI库,大家认为呢?
- super使用求解
- 当我的鼠标点击在某个网页的任意点时,我可以知道它的位置信息吗?(b/s架构)
- JComponent的printAll(Graphics g)方法怎么用?
- 我用输出流向文件中写数据,数据只能在一行上连续的写。我想在第二行写数据该用什么方法实现?
- 在java里,递归的程序怎么写??麻烦随便写个例子出来。谢谢!
- JAVA语法里的VOID到底怎么用呀?我不知道什么时候加到类或者方法里!?
- 简单计算器程序,帮忙注释下
- swt跨平台性好吗?
output.close(); // 这里已经把输出关闭了
input.close(); // 输入关闭
inputReader.close();
outputWriter.close(); // 重复关闭,也许内部实现没有考虑到重复关闭问题?我想看看你的异常输出是什么?可惜你没有贴出来,可惜了。