Day 63

_复制多级文件夹

  1. 需求:把多级文件夹复制到当前模块包下

  2. 思路:

    • 创建数据源File对象
    • 创建目的地File对象
    • 写方法实现文件夹的复制,参数为数据File对象和目的File对象
    • 判断数据源File是否为目录:
      • 是:
        • 在目的地文件下创建和数据源名称一样的目录
        • 获取数据源File下所有文件或者是目录File数组
        • 遍历该File数组,得到每一个File对象
        • 把该File作为数据源File对象,递归调用复制文件夹的方法
      • 不是:
        • 说明是文件,直接复制,用字节流
  3. package demo17;
    
    import java.io.*;
    
    public class IO_File_Demo_02 {
        public static void main(String[] args) {
            /*
            需求:把**多级目录文件夹**复制到当前模块包下
    
            思路:
                - 创建数据源File对象
                - 创建目的地File对象
                - 写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
                - 判断数据源File是否为目录
                  - 是:
                    - 在目的地下创建数据源File名称一样的目录;
                    - 获取数据源File下所有文件说这是目录的File数组
                    - 遍历该File数组,得到每一个File对象
                    - 把该File作为数据源File对象,递归调用复制文件夹的方法
                  - 不是:
                    - 说明是文件,直接复制,用字节流
             */
            // 创建数据源File对象     创建目的地File对象
            File srcFolder = new File("C:\\Users\\Alvord\\Desktop\\markdown学习\\Java练习文件");
            File destFolder = new File("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo17",srcFolder.getName());
            //
            if (!destFolder.exists()) {
                destFolder.mkdirs();
            }
            CopeFolder(srcFolder,destFolder);
    
    
    
        }
    
        public static void CopeFolder(File srcFolder, File destFolder) {
            // 判断数据源File是否为目录
            if (srcFolder.isDirectory()) {
                File[] listFiles = srcFolder.listFiles();
                // 遍历目录数组
                for (File file : listFiles) {
                    // 获取每一个文件/文件夹的名称
                    String srcName = file.getName();
                    if (file.isDirectory()) {
                        // 创建对应的目的地的**文件夹**
                        File newFile = new File(destFolder, srcName);
                        newFile.mkdirs();
                        CopeFolder(file, newFile);
                    } else {
                        // 封装目的地**文件**的路径
                        File destFile = new File(destFolder, srcName);
                        CopeFile(file,destFile);
                    }
                }
    
            } else {
                CopeFile(srcFolder,destFolder);
            }
        }
        
    
        public static void CopeFile(File srcFileName, File destFileName) {
            // 采用字符流复制文件
            BufferedInputStream bis = null;
            BufferedOutputStream bos =null;
            try {
                bis = new BufferedInputStream(new FileInputStream(srcFileName));
                bos = new BufferedOutputStream(new FileOutputStream(destFileName));
                if (bis != null) {
                    int len;
                    byte[] bytes = new byte[1024];
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    ==================================
    
    Process finished with exit code 0
    

_线程

  1. 线程:是进程中的单个顺序控制流,是一条执行路径
    • 单线程:一个进程如果只有一条执行路径,则称为单线程程序
    • 多线程:一个进程如果有有多条执行路径,则成为多线程

_继承Thread类的方法实现多线程

  1. 方式一:继承Thread类

    • 定义一个类MyThread继承Thread类
    • 在MayThread类中重写run()方法
    • 启动线程
  2. package demo18;
    
    public class Thread_Demo_01 {
        public static void main(String[] args) {
            MyThread m1 = new MyThread();
            MyThread m2 = new MyThread();
    
            /*
            public void start()   导致此线程开始执行; Java虚拟机调用此线程的run方法。
            结果是两个线程同时运行:当前线程(从调用返回到start方法)和另一个线程(执行其run方法)。
            不止一次启动线程是不合法的。
            特别地,一旦线程完成执行就可能不会重新启动。
             */
            m1.start();
            m2.start();
        }
    }
    -----------------------------------------------
    package demo18;
    
    public class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("hello world");
            // 第一个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println(i);
            }
    
            // 第二个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println("第" + i + "个数");
            }
    
            // 第三个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println(i+"个纸片人");
            }
        }
    }
    ========================================
    hello world
    0
    1
    hello world
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    2
    30个数
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    191个数
    第0个数
    第2个数
    第3个数
    第4个数
    第1个数
    第5个数
    第2个数
    第3个数
    第4个数
    第5个数
    第6个数
    第7个数
    第6个数
    第7个数
    第8个数
    第8个数
    第9个数
    第9个数
    第10个数
    第10个数
    第11个数
    第12个数
    第13个数
    第14个数
    第15个数
    第16个数
    第17个数
    第18个数
    第19个数
    第11个数
    0个纸片人
    第12个数
    1个纸片人
    第13个数
    2个纸片人
    3个纸片人
    4个纸片人
    5个纸片人
    第14个数
    6个纸片人
    7个纸片人
    8个纸片人
    第15个数
    9个纸片人
    第16个数
    10个纸片人
    第17个数
    第18个数
    第19个数
    0个纸片人
    11个纸片人
    1个纸片人
    12个纸片人
    2个纸片人
    13个纸片人
    14个纸片人
    15个纸片人
    16个纸片人
    17个纸片人
    18个纸片人
    19个纸片人
    3个纸片人
    4个纸片人
    5个纸片人
    6个纸片人
    7个纸片人
    8个纸片人
    9个纸片人
    10个纸片人
    11个纸片人
    12个纸片人
    13个纸片人
    14个纸片人
    15个纸片人
    16个纸片人
    17个纸片人
    18个纸片人
    19个纸片人
    
    Process finished with exit code 0
    

_设置和获取线程名称

  1. Thread类中的设置和获取线程名称的方法:

    • void setName(String name) :将此线程的名称更改为等于参数name
    • String getName():返回此线程的名称
  2. package demo18;
    public class Thread_Demo_02 {
        public static void main(String[] args) {
            /*
            Thread类中的设置和获取线程名称的方法:
            - void setName(String name) :将此线程的名称更改为等于参数name
            - String getName():返回此线程的名称
             */
    
            MyThread_02 m1 = new MyThread_02("飞机");
            MyThread_02 m2 = new MyThread_02("火车");
    
            m1.start();
            m2.start();
        }
    }
    ----------------------------------
    package demo18;
    
    public class MyThread_02 extends Thread{
        public  MyThread_02() {}
    
        public  MyThread_02(String name) {
            super(name);
        }
    
        @Override
        public void run() {
            // 第一个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println(getName()+":"+i);
            }
    
            // 第二个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println(getName() + "票编号:" + i);
            }
    
            // 第三个for循环
            for (int i = 0; i < 20; i++) {
                System.out.println(getName() + "索引码:" + i);
            }
    
        }
    }
    =============================================
    飞机:0
    火车:0
    飞机:1
    火车:1
    火车:2
    火车:3
    火车:4
    飞机:2
    飞机:3
    飞机:4
    飞机:5
    飞机:6
    飞机:7
    飞机:8
    飞机:9
    飞机:10
    飞机:11
    飞机:12
    飞机:13
    飞机:14
    飞机:15
    飞机:16
    飞机:17
    飞机:18
    飞机:19
    飞机票编号:0
    飞机票编号:1
    飞机票编号:2
    飞机票编号:3
    飞机票编号:4
    飞机票编号:5
    飞机票编号:6
    飞机票编号:7
    飞机票编号:8
    飞机票编号:9
    飞机票编号:10
    飞机票编号:11
    飞机票编号:12
    飞机票编号:13
    飞机票编号:14
    飞机票编号:15
    飞机票编号:16
    飞机票编号:17
    飞机票编号:18
    飞机票编号:19
    飞机索引码:0
    飞机索引码:1
    飞机索引码:2
    飞机索引码:3
    飞机索引码:4
    飞机索引码:5
    飞机索引码:6
    飞机索引码:7
    飞机索引码:8
    飞机索引码:9
    飞机索引码:10
    飞机索引码:11
    飞机索引码:12
    飞机索引码:13
    飞机索引码:14
    飞机索引码:15
    飞机索引码:16
    飞机索引码:17
    飞机索引码:18
    飞机索引码:19
    火车:5
    火车:6
    火车:7
    火车:8
    火车:9
    火车:10
    火车:11
    火车:12
    火车:13
    火车:14
    火车:15
    火车:16
    火车:17
    火车:18
    火车:19
    火车票编号:0
    火车票编号:1
    火车票编号:2
    火车票编号:3
    火车票编号:4
    火车票编号:5
    火车票编号:6
    火车票编号:7
    火车票编号:8
    火车票编号:9
    火车票编号:10
    火车票编号:11
    火车票编号:12
    火车票编号:13
    火车票编号:14
    火车票编号:15
    火车票编号:16
    火车票编号:17
    火车票编号:18
    火车票编号:19
    火车索引码:0
    火车索引码:1
    火车索引码:2
    火车索引码:3
    火车索引码:4
    火车索引码:5
    火车索引码:6
    火车索引码:7
    火车索引码:8
    火车索引码:9
    火车索引码:10
    火车索引码:11
    火车索引码:12
    火车索引码:13
    火车索引码:14
    火车索引码:15
    火车索引码:16
    火车索引码:17
    火车索引码:18
    火车索引码:19
    
    Process finished with exit code 0
    

_线程优先级

  1. 线程有;两种调度模型

    • 分时调度模型:所有线程轮流使用CPU的使用权,平均分配每个线程占用CPU的时间片
    • 抢占式调度模型:优先让优先级高的线程使用CPU,如果线程的优先级相同,那么会随机选择一个,优先级高的线程获取CPU时间片相对多一些
    • JAVA使用的是抢占式调度模型
    • 假设计算机只有一个CPU,那么CPU在某一个时刻只能执行一条指令,线程只有得到CPU时间片,也就是使用权才可以执行指令。所以说多线程程序的执行是有随机性的,因为谁抢到CPU的使用权是不一定的
    • Thread类 中设置和获取线程优先级的方法
    • public final int getPriority():返回此线程的优先级
    • public final void setPriority(int newPriority):更改此线程的优先级
  2. package demo18;
    
    public class Thread_Demo_03 {
        public static void main(String[] args) {
            /*
            - 假设计算机只有一个CPU,那么CPU在某一个时刻只能执行一条指令,线程只有得到CPU时间片,也就是使用权才可以执行指令。所以说多线程程序的执行是有**随机性**的,因为谁抢到CPU的使用权是不一定的
            - Thread类 中设置和获取线程优先级的方法
            - public final int getPriority():返回此线程的优先级
            - public final void setPriority(int newPriority):更改此线程的优先级
             */
    
            MyThread_03 m1 = new MyThread_03("火箭弹");
            MyThread_03 m2 = new MyThread_03("大炮");
            MyThread_03 m3 = new MyThread_03("核弹头");
    
            System.out.println(m1.getPriority());  // 5
            System.out.println(m2.getPriority());  // 5
            System.out.println(m3.getPriority());  // 5
            System.out.println("============================");
    
            m1.setPriority(Thread.MAX_PRIORITY);
            m1.setPriority(2);
            m1.setPriority(3);
            m1.start();
            m2.start();
            m3.start();
        }
    }
    -----------------------------------------
    package demo18;
    
    public class MyThread_03 extends Thread{
        public MyThread_03() {
        }
    
        public MyThread_03(String name) {
            super(name);
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println(getName() + "的编号是:" + i);
            }
        }
    }
    =======================================
    5
    5
    5
    ============================
    大炮的编号是:0
    大炮的编号是:1
    大炮的编号是:2
    大炮的编号是:3
    大炮的编号是:4
    大炮的编号是:5
    大炮的编号是:6
    大炮的编号是:7
    大炮的编号是:8
    大炮的编号是:9
    大炮的编号是:10
    大炮的编号是:11
    大炮的编号是:12
    大炮的编号是:13
    大炮的编号是:14
    大炮的编号是:15
    大炮的编号是:16
    大炮的编号是:17
    大炮的编号是:18
    大炮的编号是:19
    大炮的编号是:20
    大炮的编号是:21
    大炮的编号是:22
    大炮的编号是:23
    大炮的编号是:24
    大炮的编号是:25
    大炮的编号是:26
    大炮的编号是:27
    大炮的编号是:28
    大炮的编号是:29
    大炮的编号是:30
    大炮的编号是:31
    大炮的编号是:32
    大炮的编号是:33
    大炮的编号是:34
    大炮的编号是:35
    大炮的编号是:36
    大炮的编号是:37
    大炮的编号是:38
    大炮的编号是:39
    大炮的编号是:40
    大炮的编号是:41
    大炮的编号是:42
    大炮的编号是:43
    大炮的编号是:44
    大炮的编号是:45
    大炮的编号是:46
    大炮的编号是:47
    大炮的编号是:48
    大炮的编号是:49
    大炮的编号是:50
    大炮的编号是:51
    大炮的编号是:52
    大炮的编号是:53
    大炮的编号是:54
    大炮的编号是:55
    大炮的编号是:56
    大炮的编号是:57
    大炮的编号是:58
    大炮的编号是:59
    大炮的编号是:60
    大炮的编号是:61
    大炮的编号是:62
    大炮的编号是:63
    大炮的编号是:64
    大炮的编号是:65
    大炮的编号是:66
    大炮的编号是:67
    大炮的编号是:68
    大炮的编号是:69
    大炮的编号是:70
    大炮的编号是:71
    大炮的编号是:72
    大炮的编号是:73
    大炮的编号是:74
    大炮的编号是:75
    大炮的编号是:76
    大炮的编号是:77
    大炮的编号是:78
    大炮的编号是:79
    大炮的编号是:80
    大炮的编号是:81
    大炮的编号是:82
    大炮的编号是:83
    大炮的编号是:84
    大炮的编号是:85
    大炮的编号是:86
    大炮的编号是:87
    大炮的编号是:88
    大炮的编号是:89
    大炮的编号是:90
    大炮的编号是:91
    大炮的编号是:92
    大炮的编号是:93
    大炮的编号是:94
    大炮的编号是:95
    大炮的编号是:96
    大炮的编号是:97
    大炮的编号是:98
    大炮的编号是:99
    核弹头的编号是:0
    核弹头的编号是:1
    核弹头的编号是:2
    核弹头的编号是:3
    核弹头的编号是:4
    核弹头的编号是:5
    核弹头的编号是:6
    核弹头的编号是:7
    核弹头的编号是:8
    核弹头的编号是:9
    核弹头的编号是:10
    核弹头的编号是:11
    核弹头的编号是:12
    核弹头的编号是:13
    核弹头的编号是:14
    核弹头的编号是:15
    核弹头的编号是:16
    核弹头的编号是:17
    核弹头的编号是:18
    核弹头的编号是:19
    核弹头的编号是:20
    核弹头的编号是:21
    核弹头的编号是:22
    核弹头的编号是:23
    核弹头的编号是:24
    核弹头的编号是:25
    核弹头的编号是:26
    核弹头的编号是:27
    核弹头的编号是:28
    核弹头的编号是:29
    核弹头的编号是:30
    核弹头的编号是:31
    核弹头的编号是:32
    核弹头的编号是:33
    核弹头的编号是:34
    核弹头的编号是:35
    核弹头的编号是:36
    核弹头的编号是:37
    核弹头的编号是:38
    核弹头的编号是:39
    核弹头的编号是:40
    核弹头的编号是:41
    核弹头的编号是:42
    核弹头的编号是:43
    核弹头的编号是:44
    核弹头的编号是:45
    核弹头的编号是:46
    核弹头的编号是:47
    核弹头的编号是:48
    核弹头的编号是:49
    核弹头的编号是:50
    核弹头的编号是:51
    核弹头的编号是:52
    核弹头的编号是:53
    核弹头的编号是:54
    核弹头的编号是:55
    核弹头的编号是:56
    核弹头的编号是:57
    核弹头的编号是:58
    核弹头的编号是:59
    核弹头的编号是:60
    核弹头的编号是:61
    核弹头的编号是:62
    核弹头的编号是:63
    核弹头的编号是:64
    核弹头的编号是:65
    核弹头的编号是:66
    核弹头的编号是:67
    核弹头的编号是:68
    核弹头的编号是:69
    核弹头的编号是:70
    核弹头的编号是:71
    核弹头的编号是:72
    核弹头的编号是:73
    核弹头的编号是:74
    核弹头的编号是:75
    核弹头的编号是:76
    核弹头的编号是:77
    核弹头的编号是:78
    核弹头的编号是:79
    核弹头的编号是:80
    核弹头的编号是:81
    核弹头的编号是:82
    核弹头的编号是:83
    核弹头的编号是:84
    核弹头的编号是:85
    核弹头的编号是:86
    核弹头的编号是:87
    核弹头的编号是:88
    核弹头的编号是:89
    核弹头的编号是:90
    核弹头的编号是:91
    核弹头的编号是:92
    核弹头的编号是:93
    核弹头的编号是:94
    核弹头的编号是:95
    核弹头的编号是:96
    核弹头的编号是:97
    核弹头的编号是:98
    核弹头的编号是:99
    火箭弹的编号是:0
    火箭弹的编号是:1
    火箭弹的编号是:2
    火箭弹的编号是:3
    火箭弹的编号是:4
    火箭弹的编号是:5
    火箭弹的编号是:6
    火箭弹的编号是:7
    火箭弹的编号是:8
    火箭弹的编号是:9
    火箭弹的编号是:10
    火箭弹的编号是:11
    火箭弹的编号是:12
    火箭弹的编号是:13
    火箭弹的编号是:14
    火箭弹的编号是:15
    火箭弹的编号是:16
    火箭弹的编号是:17
    火箭弹的编号是:18
    火箭弹的编号是:19
    火箭弹的编号是:20
    火箭弹的编号是:21
    火箭弹的编号是:22
    火箭弹的编号是:23
    火箭弹的编号是:24
    火箭弹的编号是:25
    火箭弹的编号是:26
    火箭弹的编号是:27
    火箭弹的编号是:28
    火箭弹的编号是:29
    火箭弹的编号是:30
    火箭弹的编号是:31
    火箭弹的编号是:32
    火箭弹的编号是:33
    火箭弹的编号是:34
    火箭弹的编号是:35
    火箭弹的编号是:36
    火箭弹的编号是:37
    火箭弹的编号是:38
    火箭弹的编号是:39
    火箭弹的编号是:40
    火箭弹的编号是:41
    火箭弹的编号是:42
    火箭弹的编号是:43
    火箭弹的编号是:44
    火箭弹的编号是:45
    火箭弹的编号是:46
    火箭弹的编号是:47
    火箭弹的编号是:48
    火箭弹的编号是:49
    火箭弹的编号是:50
    火箭弹的编号是:51
    火箭弹的编号是:52
    火箭弹的编号是:53
    火箭弹的编号是:54
    火箭弹的编号是:55
    火箭弹的编号是:56
    火箭弹的编号是:57
    火箭弹的编号是:58
    火箭弹的编号是:59
    火箭弹的编号是:60
    火箭弹的编号是:61
    火箭弹的编号是:62
    火箭弹的编号是:63
    火箭弹的编号是:64
    火箭弹的编号是:65
    火箭弹的编号是:66
    火箭弹的编号是:67
    火箭弹的编号是:68
    火箭弹的编号是:69
    火箭弹的编号是:70
    火箭弹的编号是:71
    火箭弹的编号是:72
    火箭弹的编号是:73
    火箭弹的编号是:74
    火箭弹的编号是:75
    火箭弹的编号是:76
    火箭弹的编号是:77
    火箭弹的编号是:78
    火箭弹的编号是:79
    火箭弹的编号是:80
    火箭弹的编号是:81
    火箭弹的编号是:82
    火箭弹的编号是:83
    火箭弹的编号是:84
    火箭弹的编号是:85
    火箭弹的编号是:86
    火箭弹的编号是:87
    火箭弹的编号是:88
    火箭弹的编号是:89
    火箭弹的编号是:90
    火箭弹的编号是:91
    火箭弹的编号是:92
    火箭弹的编号是:93
    火箭弹的编号是:94
    火箭弹的编号是:95
    火箭弹的编号是:96
    火箭弹的编号是:97
    火箭弹的编号是:98
    火箭弹的编号是:99
    
    Process finished with exit code 0
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值