代码如下,分成2个部分的话,无论是进行copy还是进行改名都没有问题,问题就出现在一起操作的时候执行提示错误:
Exception in thread "main" java.io.FileNotFoundException: d:\day1105 (拒绝访问。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:97) at java.io.FileReader.<init>(FileReader.java:58) at exam.ReCopy.main(ReCopy.java:27)网路上参考了部分意见,不能解决。来这里请教,是因为修改后的文件还是引用指向状态,所以无法被读写复制?
package exam;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
public class ReCopy {
public static void main(String[] args) throws IOException {
File temp = new File("d:\\day1105");
File[] fileArray = temp.listFiles(new FilenameFilter() {
//这里进行文件判断,是否为文件,是否符合后缀名.java。
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
for (File file : fileArray) {
String newName = file.getName().replace(".java",".jjjj");
File newFile = new File(temp,newName);
file.renameTo(newFile);
}
BufferedReader br = new BufferedReader(new FileReader("d:\\day1105"));
BufferedWriter bw = new BufferedWriter(new FileWriter("f:\\day"));
//进行写入
int len = 0;
while((len=br.read())!=-1){
bw.write(len);
}
br.close();
bw.close();
}
}
|