题目1
递归删除某个目录下的所有的文件
public class Test4 { public static void main(String[] args) { File f = new File("D:\\练习删除目录"); delFile(f); System.out.println("删除成功!"); } static void delFile(File f) { File [] fileList = f.listFiles(); for(File file:fileList) { if(file.isDirectory()) {//isDirectory()判断是否是目录 delFile(file); }else { file.delete(); } } } }
题目2
写一个查询没交做业的人员的程序, 要求,对名单路径和作业路径,使用配置文件进行配置
public class Test5 { public static void main(String[] args) throws IOException { Reader r = new FileReader("d://名单.txt");//读出 BufferedReader br = new BufferedReader(r);//包装类里面有readLine方法 String str = ""; List<String> stuNameList = new ArrayList<>();//学生名的集合 while((str = br.readLine()) != null) {//读每一行然后加进去 stuNameList.add(str); } File f = new File("D:\\7-20 刷题"); Writer w = new FileWriter("D:\\未交作业的名单.txt"); String [] fileNameList = f.list();//返回字符数组 for(String stuName : stuNameList) {//学生的名 boolean flag = false; for(String fileName : fileNameList ) {//文件的名 if(fileName.indexOf(stuName) != -1) {//如果字符串中不含有就返回-1 flag = true; break; } } if(!flag) { w.write(stuName+"\r\n"); System.out.println("未交作业的人名:"+stuName); } } br.close(); w.close(); } }
题目3
递归取出某个文件夹中所有的文件的名称,然后存在一个集合里, 再把这个集合中的内容写到一个文件里
public class Test6 { public static void main(String[] args) throws IOException { File dir = new File("D:\\7-30 IO"); List <File> result = new ArrayList<File>(); showFile(dir,result); writeFile(result,"D:\\name.txt"); System.out.println("写入成功!"); } //把文件夹中的名称写到集合 static void showFile(File dir,List <File> result ) throws IOException { File[] fileList= dir.listFiles(); List<String> array=new ArrayList<>(); for(File file : fileList) { if(file.isDirectory()) { showFile(file,result); }else { result.add(file); } } } //把集合中的名单写到文件中 static void writeFile(List<File> files,String Filename) throws IOException { BufferedWriter bw=new BufferedWriter(new FileWriter(Filename)); for(File file :files) { String filePath = file.getAbsolutePath(); bw.write(filePath); bw.newLine(); bw.flush(); } bw.close(); } }
题目4
在某个磁盘上,有一个文件夹,里面是人员的照片,照片的在命名的时候,含有这个人的姓名信息
写一个程序,将名单中有照片的人的照片选出来,存到一个文件夹里
名单:
张三
本四
王五
photo:
夏天_张三_001.jpg
李四_2009.jpg
public static void main(String[] args) throws IOException { /* * 将名单中有照片的人的照片选出来,存到一个文件夹里 */ Reader r = new FileReader("D:/名单.txt"); BufferedReader br = new BufferedReader(r); String str = null; List<String> studentNameList = new ArrayList<>();//学生的集合 while( (str = br.readLine())!=null ) { studentNameList.add(str);//每一行读添加到集合中 } File f =new File("D:\\7-20 刷题"); String [] photoList= f.list(new FilenameFilter() {//图片的索引数组 public boolean accept(File dir, String name) { //过滤 return name.toLowerCase().endsWith(".png"); } } ); for(String stuName : studentNameList) {//学生的名 for(String phoindex : photoList) {//图片 if(phoindex.indexOf(stuName)!=-1) { // System.out.println(proindex); InputStream in =new FileInputStream("D:\\7-20 刷题/"+phoindex); OutputStream out1=new FileOutputStream("D:\\1/"+phoindex); byte [] buff=new byte [1024]; int len=0; while( (len=in.read(buff))!=-1) { out1.write(buff,0,len); } in.close(); out1.close(); } } } br.close(); } }