文件复制,递归复制
import java.io.*;
public class CopyCer {
public static void main(String[] args) {
File file = new File("D:/a");
copys(file);
}
public static void copys(File file){
if (file.isFile()){
String absolutePath = file.getAbsolutePath();
String newPath = "C"+absolutePath.substring(1);
File newFile = new File(newPath);
File newFilePath = newFile.getParentFile();
if (!newFilePath.exists()) {
newFilePath.mkdirs();
}
try(FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fileInputStream);
FileOutputStream fos = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
) {
int read;
byte[] bytes = new byte[102400];
while((read = bis.read(bytes)) != -1){
bos.write(bytes, 0, read);
}
} catch (Exception e) {
e.getMessage();
}
}else {
File[] files = file.listFiles();
for (File eve : files) {
copys(eve);
}
}
}
}
线程
创建线程
- 继承Thread类
- 实现Runnable接口
package com.Thread;
import java.util.function.DoubleToIntFunction;
public class Ticket {
public static void main(String[] args) {
Tic01 tic01 = new Tic01();
Tic01 tic02 = new Tic01();
Tic01 tic03 = new Tic01();
tic01.setName("窗口一");
tic02.setName("窗口二");
tic03.setName("窗口三");
tic01.start();
tic02.start();
tic03.start();
//接口方式
/* Tic02 tic02 = new Tic02();
new Thread(tic02).start();
new Thread(tic02).start();
new Thread(tic02).start();*/
}
}
class Tic01 extends Thread {
//多个线程共享tic_num
private static int tic_num = 100;
@Override
public void run() {
while (true) {
if (tic_num <= 1) {
// System.out.println("over");
break;
}
//sleep 50ms
synchronized (this) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("window " + Thread.currentThread().getName() + " sale one tic " + " 剩余 " + (tic_num--));
}
}
}
//使用接口方式解决
class Tic02 implements Runnable {
private int tic_num = 100;
@Override
public void run() {
while (true) {
//sleep 50ms
try {
Thread.sleep(30);
if (tic_num <= 0) {
System.out.println("over");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("window " + Thread.currentThread().getName() + " sale one tic " + " and " + (--tic_num));
}
}
}
常用方法
- getName
- setName 设置名字
- getPriority
- setPriority 设置优先级
- sleep
- interrupt
- Thread.currentThread
优先级
1-10
声明周期
新建
就绪
运行
阻塞
死亡