package com.hp.demo;
import java.io.*;
import java.util.concurrent.atomic.AtomicLong;
public class PictureUp {
public static void main(String[] args) throws IOException, InterruptedException {
//创建文件对象
File file = new File("C:\\Users\\46237\\Desktop\\test");
String s[] = file.list();
for (int i = 0; i < s.length; i++) {
// AtomicLong len = new AtomicLong();
// AtomicLong len1 = new AtomicLong();
int finalI = i;
Thread thread = new Thread(() -> {
FileInputStream inputStream1 = null;
FileOutputStream outputStream1 =null;
try {
//读取此目标目录下的图片文件
inputStream1 = new FileInputStream("C:\\Users\\46237\\Desktop\\test\\" + s[finalI]);
File file1 = new File("C:\\Users\\46237\\Desktop\\test\\" + s[finalI]);
// len.set(file1.length());
// System.out.println("len = " + len);
//将目标目录下的图片文件保存在此新目录下
outputStream1 = new FileOutputStream("C:\\Users\\46237\\Desktop\\test1\\" + s[finalI]);
int a = 0;
byte b[] = new byte[512];
//判断,调用inputStream.read ()方法,从输入流中读取一个数据,并返回这个字节。读取内容达到末尾后是否返回-1
while(true){
try {
if (!((a = inputStream1.read()) != -1)) break;
outputStream1.write(b, 0, a);
File file2 = new File("C:\\Users\\46237\\Desktop\\test1\\" + s[finalI]);
// len1.set(file2.length());
// System.out.println("len1 = " + len1);
// double cha = len1.get() / len.get();
// System.out.println(cha);
} catch (IOException e) {
e.printStackTrace();
}
//调用outputStream.write()方法字节输出流写入
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
//关闭并释放输出流资源
try {
outputStream1.close();
inputStream1.close();
} catch (IOException e) {
e.printStackTrace();
}
//关闭并释放输入流资源
}
});
thread.setName("子程序");
System.out.println(thread.getName());
thread.start();
Thread.sleep(1000);
Thread.yield();
System.out.println("线程结束");
}
}
}