3、在第一题的基础上,生成10个员工对象,创建2个线程,第一个线程将所有的2018年9月1日之前入职的男雇员信息写入到“D:\male.txt”文件中;第二个线程将所有女雇员信息写入到“D:\female.txt”文件中。
4、在第三题的基础上,创建新目录“E:\employee”,然后将“D:\male.txt”和“D:\female.txt”按序拷贝到“E:\employee\employee.txt”中。
package java3;
import java.io.*;
import java.util.ArrayList;
class Employee0 {
public String name;
public String ID;
public int age;
public String salary;
public Employee0() {};
public Employee0(String name,String ID,int age,String salary){
this.name=name ;
this.ID=ID;
this.age=age;
this.salary=salary;
}
public String getID() {
return ID;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSalary() {
return salary;
}
public String toString () {
String mess = " 姓名:" +getName()+" 年龄:"+getAge()+ " ID:"+getID()+ "薪水:"+getSalary() ;
return mess;
}
}
class runa implements Runnable{
private ArrayList arraylist;
private String path;
public runa(ArrayList arraylist,String path){
this.arraylist=arraylist;
this.path=path;
}
@Override
public void run() {
// TODO Auto-generated method stub
String mess = null;
try {
System.out.println("hello ");
File file =new File(path);
if(!file.exists()) file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
for(int i=0;i<arraylist.size();i++) {
if(((Employee0)arraylist.get(i)).getID().substring(8,10).equals("01")) {
mess="name:"+((Employee0)arraylist.get(i)).getName()+"\t"+"ID: "+((Employee0)arraylist.get(i)).getID()+"\t"+"age:"+((Employee0)arraylist.get(i)).getAge()+"\t"+"salary:"+((Employee0)arraylist.get(i)).getSalary()+"\n";
byte[] b=mess.getBytes();
fos.write(b);
}
if(((Employee0)arraylist.get(i)).getID().substring(0,8).compareTo("20180901")<0&&((Employee0)arraylist.get(i)).getID().substring(8,10).equals("00")&&"D:\\male.txt".equals(path)) {
mess="name:"+((Employee0)arraylist.get(i)).getName()+"\t"+"ID: "+((Employee0)arraylist.get(i)).getID()+"\t"+"age:"+((Employee0)arraylist.get(i)).getAge()+"\t"+"salary:"+((Employee0)arraylist.get(i)).getSalary()+"\n";
byte[] b=mess.getBytes();
fos.write(b);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Employee{
@SuppressWarnings("unchecked")
public static void main(String []args) throws Exception{
//ArrayList<Employee> arraylist=new ArrayList<Employee>();
Employee0[] em=new Employee0[10];
em[1] =new Employee0("zengtao","2017010100222",22,"5800");
em[2] =new Employee0("zengtao1","2017010101223",22,"5800");
em[3] =new Employee0("zengtao2","2019010101232",22,"5800");
em[4] =new Employee0("zengtao3","2019010101256",22,"5800");
em[5] =new Employee0("zengtao4","2019010101256",22,"5800");
em[6] =new Employee0("zengtao5","2019010100256",22,"5800");
em[7] =new Employee0("zengtao6","2020010100289",22,"5800");
em[8] =new Employee0("zengtao7","2019010200200",22,"5800");
em[9] =new Employee0("zengtao8","2011010100212",22,"5800");
em[0] =new Employee0("zengtao9","1999010100222",22,"5800");
for(int i=0;i<10;i++) {
if(em[i].getID().substring(0,8).compareTo("20180901")<0&&em[i].getID().substring(8,10).equals("00")) {
System.out.println("name:"+em[i].getName()+"\t"+"ID:"+em[i].getID()+"\t"+"age:"+em[i].getAge()+"\t"+"salary:"+em[i].getSalary());
}
}
ArrayList arraylist =new ArrayList();
runa r1=new runa(arraylist,"D:\\male.txt");
runa r2=new runa(arraylist,"D:\\female.txt");
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
for(int i=0;i<em.length;i++)
arraylist.add(em[i]);
t1.start();
t2.start();
File file1=new File("E:\\employee");
File file2=new File("E:\\employee\\employee.txt");
File file3=new File("D:\\male.txt");
File file4=new File("D:\\female.txt");
if(!file1.exists()) file1.mkdir();
if(!file2.exists()) {
file2.createNewFile();
}
FileInputStream fis =new FileInputStream(file3);
FileInputStream fis1 =new FileInputStream(file4);
FileOutputStream fos=new FileOutputStream(file2);
byte[] b =new byte[1000];
fis.read(b,0,1000);
fis1.read(b,0,1000);
fos.write(b,0,1000);
}
}