Job_NDay_HomeWork_IOStream

1.键盘输入一个文件夹路径,将其中的文件拷贝到另一个文件夹下.只拷贝.java文件

public class Test1 {
    public static void main(String[] args) throws IOException {
        //键盘录入资源文件夹和目标文件夹
        File src = getPath();
        File desc = getPath();
        //调用方法
        copy(src, desc);
    }

    //对键盘录入的文件夹路径进行判断
    public static File getPath() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个文件夹的路径");
        while (true) {
            String path = sc.next();
            //判断这个路径是否存在
            File file = new File(path);
            if (!file.exists()) {
                System.out.println("你输入的文件夹不存在");
            } else if (file.isFile()) {
                System.out.println("你输入的不是文件夹路径");
            } else {
                return file;
            }
        }
    }

    //将文件夹下的.java文件拷贝到指定文件夹(src是资源文件夹,desc是目标文件夹)
    public static void copy(File src, File desc) throws IOException {
        //遍历源文件
        File[] files = src.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".java")) {
                    //创建流对象
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(desc, file.getName())));
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, len);
                    }
                    //释放资源
                    bis.close();
                    bos.close();
                } else {
                    copy(file, desc);       //递归操作
                }
            }
        }
    }
}

2.已知项目根目录下student_info.txt文件中有如下数据(姓名-年龄-总分):
lucy-28-98 lily-23-97 robt-25-100 wili-15-100 klin-29-93
运用IO技术获取将该文件中的数据分别封装成5个Student(姓名为String类型,年龄为int类型,总分为int类型
对象存入TreeSet集合中(需要自己定义Student类) 要求:
根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。 按照排序完的顺序将所有信息打印到控制台上: 打印格式如下:

robt-25-100 
wili-15-100 
lucy-28-98 
lily-23-97 
klin-29-93
public class Test2 {
    public static void main(String[] args) throws IOException {
        //创建集合对象
        TreeSet<Student> set = new TreeSet<>();
        //创建输入流对象
        BufferedReader br = new BufferedReader(new FileReader("test\\student_info.txt"));
        String str;
        while ((str = br.readLine()) != null) {
            String[] split = str.split("-");
            set.add(new Student(split[0], Integer.valueOf(split[1]), Integer.valueOf(split[2])));
        }
        //将TreeSet集合转换成ArrayList集合
        ArrayList<Student> list = new ArrayList<>(set);
        //使用Collections中的sort()方法
        Collections.sort(list, new MyCompare());
        //遍历集合
        for (Student student : list) {
            System.out.println(student);
        }
    }
}

public class MyCompare implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        //根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。
        if (o2.getScore() - o1.getScore() == 0) {
            return o2.getAge() - o1.getAge();
        }
        return o2.getScore() - o1.getScore();
    }
}


3.编写一个方法:查询文件夹内指定扩展名文件的个数
方法描述:将用户输入的路径以及指定的扩展名传入该方法中,返回该目录下所有符合该扩展名文件的个数
比如:输入D:\aaa,文件扩展名为.jpg 那么需要统计在D:\aaa中所有扩展名为.jpg文件的个数(子文件夹中符合条件的文件也要统计进去)

public class Test3 {
    public static void main(String[] args) {
        File f = new File("D:\\aaa");
        File[] files = f.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(".jpg")) {
                    return true;
                }
                return false;
            }
        });
        System.out.println(files.length);
    }

}

4.给定一个list集合:{“张柏芝”,“刘德华”,“张亮”,“张靓颖”,“杨颖”,“黄晓明”}
编写一个方法:将list集合中所有姓张的人员写入到D:\a.txt中
编写一个方法:将D:\a.txt中所有姓张的人员信息读取出来并打印到控制台上

public class Test4 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<String>();
        list.add("张柏芝");
        list.add("刘德华");
        list.add("张亮");
        list.add("张靓颖");
        list.add("杨颖");
        list.add("黄晓明");
        outputZhang(list);
        BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }

    private static void outputZhang(ArrayList<String> list) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\a.txt"));
        for (String s : list) {
            if (s.contains("张")) {
                bw.write(s);
                bw.newLine();
            }
        }
        bw.close();
    }
}

5.模拟歌曲复制和删除过程:假设在D:\songs文件夹中存在一些歌曲(均为mp3格式)
循环显示该菜单:请选择您要进行的操作:1:查询所有歌曲 2:根据歌曲名称复制 3:根据歌曲名称删除 4: 退出

举例:
        用户输入:1
        存在以下歌曲:
        蒙娜丽莎的眼泪
        烟花易冷
        上海滩
        小苹果
        夜空中最亮的星

        用户输入:2
        请输入要复制的歌曲名称: 上海滩
        请输入存储路径: E:\\songs  (说明:该路径如果存在则不创建,不存在则创建)
        复制结果: 歌曲上海滩已经成功复制到E:\\songs目录中

        用户输入:3
        请输入要删除的歌曲名称: 上海滩
        删除结果: 歌曲上海滩已经成功删除

        用户输入: 4
        退出系统

https://blog.csdn.net/gutie_bartholomew/article/details/79177249第五题引用地址

public class Test5 {
    static final String songsPath = "songs";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择您要进行的操作:1:查询所有歌曲   2:根据歌曲名称复制  3:根据歌曲名称删除 4: 退出");
            System.out.print("用户输入:");
            switch (scanner.next()) {
                case "1":
                    findAll();
                    break;
                case "2":
                    System.out.print("请输入要复制的歌曲名称:");
                    String name = scanner.next();
                    System.out.print("请输入存储路径:");
                    String path = scanner.next();
                    copy(name, path);
                    break;
                case "3":
                    System.out.print("请输入要删除的歌曲名称:");
                    delete(scanner.next());
                    break;
                case "4":
                    return;
                default:
                    System.out.println("指令输入有误!");
            }
        }
    }

    private static void findAll() {
        System.out.println("存在以下歌曲:");
        File folder = new File(songsPath);
        for (File file : folder.listFiles()) {
            String name = file.getName();
            name = name.substring(0, name.indexOf('.'));
            System.out.println(name);
        }
    }

    private static void copy(String name, String path) {
        //创建目的目录
        File destFolder = new File(path);
        try (//创建字节输入流 读文件
             BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(find(name).toString()));
             //创建字节输出流 写文件
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path, find(name).getName())));) {
            //创建缓冲流数组
            byte[] bytes = new byte[8192];
            int len = 0;
            //复制  一边读一边写
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (new File(path, find(name).getName()).exists())
            System.out.println("复制结果: 歌曲上海滩已经成功复制到" + path + "目录中");
    }

    private static void delete(String name) {
        File file = new File(songsPath + "//" + name + ".mp3");
        if (file.exists()) {
            file.delete();
            System.out.println("删除结果: " + name + "已经成功删除");
        } else {
            System.out.println("歌曲不存在!");
        }
    }

    private static File find(String name) {
        File file = new File(songsPath + "//" + name + ".mp3");
        if (!file.exists())
            System.out.println("歌曲不存在!");
        return file;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值