补充一些用到前面没提到的方法
isDirectory()方法,检查一个对象是否是文件夹,是true不是false
isFile()方法,检测一个对象是否为文件,是true不是false
文件的读写操作实践
上一篇大致讲了文件读写操作的基本操作,下面是实践时间
查找文件并删除
public class Demo {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入查找的目录:");String rootDir = scanner.next();File rootFile = new File(rootDir);if(!rootFile.isDirectory()){System.out.println("不是目录");}System.out.println("请输入要删除的关键字:");String keyword = scanner.next();scanDir(rootFile,keyword);}
//遍历目录private static void scanDir(File rootFile, String keyword) {File[] files = rootFile.listFiles();//列出当前目录列表if(files == null){return;//目录为空}for (File file:files){//对比,判断是文件还是目录System.out.println("遍历目录文件"+ file.getAbsolutePath());if (file.isFile()){dealFile(file,keyword);//是文件,判断文件名是否包含删除的关键字}else{scanDir(file,keyword);//目录,则继续递归}}}private static void dealFile(File file, String keyword) {if(file.getName().contains(keyword)){//contains会比对传入file与keyword是否有对部分System.out.println("发现文件:"+ file.getAbsolutePath());//绝对路径System.out.println("是否删除:y/n");Scanner x = new Scanner(System.in);String y = x.next();if (y.equalsIgnoreCase("y")){file.delete();System.out.println("删除成功");}else{System.out.println("没有删除");}}}
}
将文件并且复制到其他目录
public class Demo13 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入源文件路径:");String srcPath = scanner.next();System.out.println("请输入目标文件路径:");String destPath = scanner.next();File srcfile = new File(srcPath);if(!srcfile.isFile()){//判断是否为文件System.out.println("不是源文件或不存在");}File destFil = new File(destPath);if(!destFil.getParentFile().isDirectory()){//判断目标文件目录存不存在System.out.println("目标文件目录不存在");return;}//开始复制文件try(InputStream inputStream = new FileInputStream(srcfile) ;OutputStream outputStream = new FileOutputStream(destFil)){//前面讲要关闭文件我却没有用,这里解释一下是因为try()方法自带了关闭文件功能while(true){byte[] buf = new byte[1024];int n = inputStream.read(buf);if(n == -1){break;}outputStream.write(buf, 0 ,n);}System.out.println("复制成功");} catch (IOException e) {throw new RuntimeException();}}
}
扫描目录,找到名称或者包含指定字符的所有普通文件(不包含目录)
public class Demo14 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入要搜索的路径");String rootPath = scanner.next();File rootfile = new File(rootPath);if (!rootfile.isDirectory()){System.out.println("不是路径");return;}System.out.println("输入搜索关键字");String keyword = scanner.next();scanDir(rootfile,keyword);//扫描}private static void scanDir(File rootfile, String keyword) {File[] files = rootfile.listFiles();//获取当前目录下所有的内容if (files ==null){return;}for (File file : files){if(file.isFile()){//文件deaFile(file,keyword);}else {//目录scanDir(file,keyword);//递归}}}private static void deaFile(File file, String keyword) {if (file.getName().contains(keyword)){System.out.println("文件名包含关键字" + file.getAbsolutePath());return;}StringBuilder stringBuilder = new StringBuilder();try(Reader reader = new FileReader(file)) {//keyword是字符串,使用字符流while(true){char[] chars = new char[1024];int n = reader.read(chars);if (n ==-1){break;}stringBuilder.append(chars,0,n);}}catch(IOException e){throw new RuntimeException(e);}if(stringBuilder.indexOf(keyword)>= 0){//找到匹配项System.out.println("文件内容包含关键字" + file.getAbsolutePath());return;}}