• Java文件流练习


    1  扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件

    1. import java.io.File;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scanner=new Scanner(System.in);
    6. //先让用户输入一个目录
    7. System.out.println("请输入你要扫描的目录");
    8. String path=scanner.next();
    9. File file=new File(path);
    10. //验证输入合法性
    11. if(!file.isDirectory()){
    12. System.out.println("输入文件路径有误");
    13. return;
    14. }
    15. //让用户输入查询关键字
    16. System.out.println("请输入查询的关键字");
    17. String word=scanner.next();
    18. //开始扫描
    19. scanDir(file,word);
    20. }
    21. private static void scanDir(File file, String word) {
    22. if(file==null)return;
    23. //先列出file的目录和文件
    24. File[] path=file.listFiles();
    25. for (File f:path) {
    26. //日志
    27. System.out.println(f.getAbsolutePath());
    28. if(f.isFile()){
    29. //是文件,看看有没有包含关键字
    30. if(f.getName().contains(word)){
    31. System.out.println("当前扫描的文件是"+f.getAbsolutePath()+"删除输入Y,不删除输入N");
    32. String user=new Scanner(System.in).next();
    33. if(user.equals("Y")||user.equals("y")){
    34. f.delete();
    35. System.out.println("删除成功");
    36. }
    37. else System.out.println("删除失败");
    38. }
    39. }
    40. else {
    41. scanDir(f,word);
    42. }
    43. }
    44. }
    45. }

     

    2  进行普通文件的复制

     

    1. import java.io.*;
    2. import java.util.Scanner;
    3. //文件流操作
    4. public class Main {
    5. public static void main(String[] args) throws IOException {
    6. Scanner scanner=new Scanner(System.in);
    7. System.out.print("请输入要复制的文件(绝对路径 OR 相对路径): ");// E:\Test\bbb\222.txt
    8. String souPath=scanner.next();
    9. File soutfile=new File(souPath);
    10. if(soutfile==null)return;
    11. if(!soutfile.exists()){
    12. System.out.println("文件不存在,请确认路径是否正确");
    13. return;
    14. }
    15. if(!soutfile.isFile()){
    16. System.out.println("输入有误");
    17. return;
    18. }
    19. System.out.println("请输入要复制到的目标目录");//E:\Test\aaa\222.txt
    20. String destPath=scanner.next();
    21. File destFile=new File(destPath);
    22. if(destFile.exists()){//不能直接复制到目录下,要输入目录+目标文件名字
    23. if(destFile.isDirectory()){
    24. System.out.println("目标路径已经存在,并且是一个目录,请确认路径是否正确");
    25. return;
    26. }
    27. }
    28. if(destFile.isFile()){
    29. System.out.println("目标文件已经存在");
    30. return;
    31. }
    32. try(InputStream is = new FileInputStream( soutfile)) {
    33. try(OutputStream os=new FileOutputStream(destFile)){
    34. Scanner sc=new Scanner(is);
    35. PrintWriter writer=new PrintWriter(destFile);
    36. writer.println(sc);
    37. writer.flush();
    38. }
    39. }
    40. System.out.println("复制成功");
    41. }
    42. }

     

  • 相关阅读:
    Window10运行onnxruntime报错的处理
    C++11补充:智能指针如std::unique_ptr如何添加自定义的deleter
    Java 类型信息详解和反射机制
    设计模式学习笔记(九)桥接模式及其应用
    【Linux】——select详解
    Mysql 子查询,最值查询
    如何写好一篇技术型文档?
    MybatisPlus 快速开发
    【Pytorch深度学习开发实践学习】【VGG】经典算法复现-Pytorch实现VGG主干网络(1)model.py
    【Harmony3.1/4.0】笔记六-对话框
  • 原文地址:https://blog.csdn.net/weixin_54783024/article/details/138159352