复制文件到指定文件夹java.io.FileNotFoundException: list.txt (系统找不到指定的文件。)
运行结果:没有找到该文件!list.txt (系统找不到指定的文件。)
at (Native Method)
at (Unknown Source)
at (Unknown Source)
at (Unknown Source)
at ReadList.main(ReadList.java:20)
Exception in thread "main" java.lang.NullPointerException
at (Unknown Source)
at FileCpy.copyDir(FileCpy.java:33)
at ReadList.main(ReadList.java:32)
import
import
import
import
import java.util.ArrayList;
public class ReadList{
public static ArrayList<FileCpy> fileList;
private static String src;
private static String bak;
public static void main(String[] args) throws IOException{
BufferedReader br = null;
//捕捉异常,如果list.txt不存在,则执行catch,输出“没有找到该文件!”
try {
br = new BufferedReader(new FileReader("list.txt"));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
reader("list.txt");
} catch (FileNotFoundException e){
e.printStackTrace();
System.out.println("没有找到该文件!");
}
if(FileCpy.copyDir(src, bak)){
for(FileCpy fil:fileList){
fil.copyFile();
}
} else{
System.out.println("不能找到src目录");
}
}
//读取文件中的内容,将每行的文件作为动态数组中的元素
public static void reader(String fileName) throws IOException{
FileReader file=new FileReader(fileName);
BufferedReader br=new BufferedReader(file);
fileList=new ArrayList<FileCpy>();
while(br.readLine()!=null){
FileCpy file1=new FileCpy(br.readLine());
fileList.add(file1);
}
}
}
import *;
public class FileCpy {
private String fileName;
private String bak;
public FileCpy(String name){
setFileName(name);
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
//复制文件
public void copyFile()throws IOException{
File first=new File(fileName);
File second=new File(bak,fileName);
if(!first.exists()) {
System.out.println("该文件不存在!");
} else{
FileInputStream fis = new FileInputStream(first);
byte[] buff = new byte[1024];
FileOutputStream fos = new FileOutputStream(second);
while (fis.read(buff) != -1) {
fos.write(buff);
}
}
}
//判断src目录是否存在,建立bak目录,返回是否存在src
public static boolean copyDir(String src,String bak){
File srcDir=new File(src);
File bakDir=new File(bak);
if(srcDir.exists()){
if(!bakDir.exists()){
bakDir.mkdir();
}
}
return true;
}
}
找错误太痛苦,看英语更痛苦,我还是会找,也希望你能帮我