黑马程序员技术交流社区
标题:
关于复制多级目录下指定文件并改名的问题
[打印本页]
作者:
白衣布士
时间:
2015-2-14 14:36
标题:
关于复制多级目录下指定文件并改名的问题
我写了一个复制多级目录下指定文件并改名的程序,虽然可以实现功能但是抛出了空指针异常,大家帮我看看怎么解决
/**
* 需求:复制多级目录下指定文件并改名
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
// 需要复制的目录
String srcPath = "D:\\aaa";
// 目标目录
String destPath = "E:\\AAA";
copyFile(srcPath, destPath);
}
public static void copyFile(String srcPath, String destPath)
throws IOException {
// 封装源目录,目标目录
File srcFile = new File(srcPath);
File destFile = new File(destPath);
File[] fileArray = srcFile.listFiles();
// 判断目标目录是否存在
if (!destFile.exists()) {
destFile.mkdir();
}
for (File file : fileArray) {
if (file.isFile() && file.getName().endsWith(".java")) {
// 改名
String newName = file.getName().replace(".java", ".jad");
File newFile = new File(destFile, newName);
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
} else {
// 返回该目录的绝对路径,做为源目录传入
String newSrcPath = file.getAbsolutePath();
copyFile(newSrcPath, destPath);
}
}
}
}
复制代码
作者:
万合天宜
时间:
2015-2-14 18:05
/*
* 需求:复制多极文件夹
*
* 数据源:E:\\aaa
* 目的地:E:\\AAA
*
* 分析:
* A:封装数据源File
* B:封装目的地File
* C:判断该File是文件夹还是文件
* a:是文件夹
* 就在目的地目录下创建该文件夹
* 获取该File对象下的所有文件或者文件夹File对象
* 遍历得到每一个File对象
* 回到C
* b:是文件
* 就复制(字节流)并改名字
*/
public class CopyFolders {
public static void main(String[] args) throws IOException {
// 封装数据源File
File srcFile = new File("E:\\aaa");
// 封装目的地File
File destFile = new File("E:\\AAA");
// 复制文件夹的功能
copyFolder(srcFile, destFile);
}
private static void copyFolder(File srcFile, File destFile)
throws IOException {
// 判断该File是文件夹还是文件
if (srcFile.isDirectory()) {
// 文件夹
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir();
// 获取该File对象下的所有文件或者文件夹File对象
File[] fileArray = srcFile.listFiles();
for (File file : fileArray) {
copyFolder(file, newFolder);
}
} else {
// 文件
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile, newFile);
}
}
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
复制代码
作者:
白衣布士
时间:
2015-2-14 21:15
万合天宜 发表于 2015-2-14 18:05
好像也抛异常了
作者:
白衣布士
时间:
2015-2-14 21:21
万合天宜 发表于 2015-2-14 18:05
多了个判断是否存在目标目录,不存在就创建
作者:
cly成子
时间:
2015-2-14 21:45
不懂,求高人指点!
作者:
刘立峰
时间:
2015-2-14 22:14
支持下,混点黑马币
作者:
你的微笑很美
时间:
2015-2-14 22:40
万合天宜 发表于 2015-2-14 18:05
好复杂的说
作者:
huangchunwei
时间:
2015-2-14 23:09
看的晕了。。。
作者:
枪杆不如笔杆
时间:
2015-2-15 09:13
也在找这道题,收藏下
作者:
rehan
时间:
2015-2-15 09:25
空指针异常?那肯定是对象的问题?第一感觉就是对象指向了一个null!!应该是目录或文件问题。。。。
作者:
Imp_x
时间:
2015-2-15 09:32
把你else里面的代码换成这个
String str = file.getName;
File newFile = File(destPath,str);
newFile.mkdir();
copyFile(file,newFile);
复制代码
应该看得懂吧{:3_66:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2