04javase之文件和io流案例

1、

键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)

package demo0904;

import org.junit.Test;

import java.io.File;
import java.util.Scanner;

/**
 * @creat 2020-09-05-15:26
 */
public class Test2 {
    //键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入文件夹路径:");
        String path = scanner.next();
        File file = new File(path);
        deleteAll(file);


    }
    public static void deleteAll(File f){


        File[] files = f.listFiles();
        if (files==null){
            f.delete();
            return;
        }
        for (File ff:files){
            if (ff.isDirectory()){
                deleteAll(ff);
            }else{
                ff.delete();
            }
        }
        f.delete();

    }
}

2、

键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
例如:.txt有10个,.java有30个…

package demo0904;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @creat 2020-09-05-15:58
 * 2、键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
 * 	例如:.txt有10个,.java有30个......
 */


public class Test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入文件夹路径:");
        String path = scanner.next();
        File file = new File(path);
        HashMap<String, Integer> map = new HashMap<>();
        Map<String, Integer> ma = count(file, map);
        Set<Map.Entry<String, Integer>> set = ma.entrySet();
        for (Map.Entry<String, Integer> m:set){
            System.out.println("."+m.getKey()+"---"+m.getValue());
        }
    }
    public static Map<String, Integer> count(File file,Map<String, Integer> map){
        File[] files = file.listFiles();
        for(File f:files){
            if (f.isDirectory()){
                count(f,map);
            }else{
                String[] split = f.getName().split("[.]");
                String name = split[0];
                String sign = split[1];

                if(!map.containsKey(sign)){
                    map.put(sign,1);
                }else{
                    Integer num = map.get(sign);
                    map.put(sign,num+1);
                }
            }
        }
        return map;
    }
}

3、

键盘录入一个文件夹路径,作为源文件夹;键盘录入一个文件夹路径,作为目标文件夹
写代码将源文件夹拷贝到目标文件夹中
a
b
b/a

package demo0904;

import java.io.File;
import java.util.Scanner;

/**
 * @creat 2020-09-05-16:19
 * 键盘录入一个文件夹路径,作为源文件夹;键盘录入一个文件夹路径,作为目标文件夹
 * 	写代码将源文件夹拷贝到目标文件夹中
 *
 * 	a
 * 	b
 * 	b/a
 */
public class Test4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入源文件夹路径:");
        String path = scanner.next();
        System.out.println("输入目标文件夹路径:");
        String path1 = scanner.next();
        File file = new File(path);
        String fileName = file.getName();
        path1=path1+"/"+fileName;
        File file1 = new File(path1);
        file.renameTo(file1);
    }

}

4

在D盘下的a/b/c/d中创建一个文件,名称叫HelloWorld.txt

package demo0904;

import java.io.File;
import java.io.IOException;

/**
 * @creat 2020-09-05-16:26
 * 在D盘下的a/b/c/d中创建一个文件,名称叫HelloWorld.txt
 */
public class Test5 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:a/b/c/d/HelloWorld.txt");
        file.getParentFile().mkdirs();
        file.createNewFile();
    }
}

5

键盘录入一个字符串,表示一个文件夹路径,如果不是文件夹路径则提示重新录入 打印当前文件夹下,所有的大于20M的后缀名是.wmv的文件的绝对路径

package demo0904;

import java.io.File;
import java.util.Scanner;

/**
 * @creat 2020-09-05-16:32
 * 键盘录入一个字符串,表示一个文件夹路径,如果不是文件夹路径则提示重新录入
 *   打印当前文件夹下,所有的大于20M的后缀名是.wmv的文件的绝对路径
 */
public class Test6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean flag=true;
        while (flag){
            System.out.println("输入文件夹路径:");
            String path = scanner.next();
            File file = new File(path);
            if(!file.isDirectory()){
                System.out.println("重新输入!");
            }else{
                print(file);
                flag=false;
            }
        }

    }
    public static void print(File file){
        File[] files = file.listFiles();
        for (File f:files){
            if (f.isDirectory()){
                print(f);
            }else{
                if (f.getName().endsWith(".wmv")&&f.length()>20*1024*1024){
                    System.out.println(f.getAbsolutePath());
                }
            }
        }

    }
}

6

用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//…
(3)按照上面的打印格式将内容写入到D:\count.txt文件中(要求用高效流)

package demo0904;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @creat 2020-09-05-16:54
 * 用代码实现以下需求
 * 	(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
 * 	(2)打印格式:
 * 		to=3
 * 		think=1
 * 		you=2
 * 		//........
 * 	(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
 */
public class Test7 {
    public static void main(String[] args) throws IOException {
        String str="If you want to change your fate I think you must come to the ujiuye to learn java";
        String[] split = str.split(" ");
        HashMap<String, Integer> map = new HashMap<>();
        for (String s:split){
            if (!map.containsKey(s)){
                map.put(s,1);
            }else {
                Integer count = map.get(s);
                map.put(s,count+1);
            }
        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        PrintStream printStream = new PrintStream("D:\\\\count.txt");
        BufferedOutputStream os = new BufferedOutputStream(printStream);
        for(Map.Entry<String, Integer> m:entries){
            String s=m.getKey()+"----"+m.getValue()+"\n";
            os.write(s.getBytes());
        }

        os.close();
    }
}

7

产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中

package demo0904;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * @creat 2020-09-05-19:07
 * 产生10个1-100的随机数,并放到一个数组中
 * 	(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
 * 	(2)把数组中的数字放到当前文件夹的number.txt文件中
 */
public class Test8 {
    public static void main(String[] args) throws IOException {
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        int[] array = new int[10];
        BufferedWriter writer = new BufferedWriter(new FileWriter("E:\\code\\bigdata\\base1\\demo1\\src\\demo0904\\number.txt"));
        for (int i=1;i<=10;i++){
            int tmp = random.nextInt(100) + 1;
            array[i-1]=tmp;
            if(tmp>=10){
                list.add(tmp);
            }
            writer.write(tmp+"\t");
        }
        System.out.println(list);
        writer.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值