import java.io.*;
import java.util.ArrayList;
public class FileCpy {
private String fileName ;
public ArrayList<FileCpy>fileList ;
public FileCpy(String name){
setFileName(name);
}
public void setFileName(String name) {
this.fileName = name;
}
//复制文件
public void copyFile()throws IOException{
File first= new File("src" +"\\" +this.fileName);
File second= new File("src" +"\\" +"bak" +"\\" +this.fileName);
if(!first.exists()) {
System. out.println("内容文件不存在!" +this.fileName);
} 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("src" +"\\" +"bak" );
if(srcDir.exists()){
if(!bakDir.exists()){
bakDir.mkdir();
}
}
return true ;
}
//读取文件中的内容,将每行的文件作为动态数组中的元素
//读取文件中的内容,将每行的文件作为动态数组中的元素
public void reader(String FileName) throws IOException{
FileReader file= new FileReader(new File("src"+"\\" +fileName ));
BufferedReader br= new BufferedReader(file);
this.fileList =new ArrayList<FileCpy>();
String line= "";
while((line=br.readLine())!=null){
FileCpy file1= new FileCpy(line);
fileList.add(file1);
}
}
public void allCopy() throws IOException{
if(FileCpy.copyDir( "src", "bak" )){
for(FileCpy fil:this.fileList){
fil.copyFile();
}
} else{
System. out.println("不能找到src目录" );
}
}
}
import java.io.*;
public class ReadList{
public static void main(String[] args) throws IOException{
BufferedReader br = null;
FileCpy list= new FileCpy("list.txt" );
//捕捉异常,如果list.txt不存在,则执行catch,输出“没有找到该文件!”
try {
br = new BufferedReader(new FileReader("src" +"\\" +"list.txt" ));
String line = "";
while ((line = br.readLine()) != null) {
System. out.println(line);
}
list.reader( "list.txt");
} catch (FileNotFoundException e){
System. out.println("没有找到list.txt!" );
e.printStackTrace();
}
list.allCopy();
}
}