1.定义一个雇员类Employee,包括属性:姓名,工号,入职日期,工资。生成20个员工对象,存储到ArrayList中,找出所有在2018年1月1号之前入职的员工,并显示;
2.在第一题的基础上,生成10个员工对象,创建2个线程,第一个线程将所有的2018年9月1日之前入职的男雇员信息写入到“D:\male.txt”文件中;第二个线程将所有女雇员信息写入到“D:\female.txt”文件中。
3.在第二题的基础上,创建新目录“E:\employee”,然后将“D:\male.txt”和“D:\female.txt”按序拷贝到“E:\employee\employee.txt”中。
编写本题代码遇到的问题:主函数中对象数组中值无法直接传给线程类,着重关注下arrayList类传对象数组的用法:
主函数中:
ArrayList arrayList=new ArrayList();
A1 ds1=new A1("A","male.txt","D:\\","2018-09-01","男",arrayList);
A1 ds2=new A1("B","female.txt","D:\\","2018-09-01","女",arrayList);
Thread t1=new Thread(ds1);
Thread t2=new Thread(ds2);
for(int i=0;i<AList.length;i++)
arrayList.add(AList[i]);//加入对象
调用类中方法使用::
private ArrayList arrayList;//声明
this.arrayList=arrayList;//初始化
(Employee)arrayList.get(i)
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
class Employee{
private String name;
private int id;
private String enterDate;
private int salary;
private String sex;
Employee(){};
Employee(String name,int id,String enterDate,int salary,String sex){
this.name=name;
this.id=id;
this.enterDate=enterDate;
this.salary=salary;
this.sex=sex;
}
public String getName1(){
return name;
}
public int getID(){
return id;
}
public String getEnterdate(){
return enterDate;
}
public int getSalary(){
return salary;
}
public String getSex(){
return sex;
}
}
class A1 implements Runnable{
private String ThreadName;
private String FileName;
private String FilePath;
private String date;
private String sex;
private ArrayList arrayList;
private String content;
public A1(String ThreadName,String FileName,String FilePath,String date, String sex,ArrayList arrayList) {
this.ThreadName=ThreadName;
this.FileName=FileName;
this.FilePath=FilePath;
this.date=date;
this.sex=sex;
this.arrayList=arrayList;
}
@Override
public void run() {
// TODO Auto-generated method stub
content="";
try {
File file=new File(FilePath+FileName);
if(!file.exists())
file.createNewFile();
FileOutputStream fout=new FileOutputStream(file); //创建文件输出流
for(int i=0;i<arrayList.size();i++){
if((((Employee)arrayList.get(i)).getEnterdate()).compareTo(date)<0 &&
((Employee)arrayList.get(i)).getSex()==sex){
content=content+((Employee)arrayList.get(i)).getName1()+"\t"+((Employee)arrayList.get(i)).getID()+"\t"+((Employee)arrayList.get(i)).getEnterdate()+"\t"+((Employee)arrayList.get(i)).getSalary()+"\r\n";
}
}
byte[] b=content.getBytes();
fout.write(b);
System.out.println("文件写入成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.getMessage();
}
}
}
public class Employee1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee employee1=new Employee("Jerry1001",1001,"2017-01-09",3000,"男");
Employee employee2=new Employee("Jerry1002",1002,"2016-01-09",9000,"女");
Employee employee3=new Employee("Jerry1003",1003,"2017-08-09",4000,"男");
Employee employee4=new Employee("Jerry1004",1004,"2018-01-09",3000,"女");
Employee employee5=new Employee("Jerry1005",1005,"2018-10-09",5000,"女");
Employee employee6=new Employee("Jerry1006",1006,"2017-01-29",6000,"男");
Employee employee7=new Employee("Jerry1007",1007,"2019-02-09",4000,"女");
Employee employee8=new Employee("Jerry1008",1008,"2017-06-09",4500,"女");
Employee employee9=new Employee("Jerry1009",1009,"2015-03-09",3200,"男");
Employee employee10=new Employee("Jerry1010",1010,"2017-01-09",4200,"女");
Employee[] AList={employee1,employee2,employee3,employee4,employee5,employee6,employee7,employee8,employee9,employee10};
for (int i=0;i<AList.length;i++){
if (AList[i].getEnterdate().compareTo("2018-09-01")<0){
System.out.println(" NAME:"+AList[i].getName1()+" ID:"+AList[i].getID()+" 入职日期:"+AList[i].getEnterdate()+" 工资:"+AList[i].getSalary());
}
}
ArrayList arrayList=new ArrayList();
A1 ds1=new A1("A","male.txt","D:\\","2018-09-01","男",arrayList);//注意arrayList引用传对象数组的用法
A1 ds2=new A1("B","female.txt","D:\\","2018-09-01","女",arrayList);
Thread t1=new Thread(ds1);
Thread t2=new Thread(ds2);
for(int i=0;i<AList.length;i++)
arrayList.add(AList[i]);//加入对象
t1.start();
while(true){
if(!t1.isAlive()) {
t2.start();
break;
}
}
File File_1=new File("D:\\","male.txt");
File File_2=new File("D:\\","female.txt");
File dDirFile=new File("E:\\employee");
File dFile=new File("E:\\employee\\employee.txt");
if(!dDirFile.exists()) dDirFile.mkdir(); //如果目录不存在则创建目录
copyFile(File_1, dFile); //复制sFile_1到dFile
copyFile(File_2, dFile); //复制sFile_2到dFile
}
static void copyFile(File sFile,File dFile) { //复制文件函数
try {
if(!sFile.exists()){
System.out.println("被复制的文件不存在");
System.exit(1);
}
if(!dFile.exists())
dFile.createNewFile();
InputStream fis=new FileInputStream(sFile);
OutputStream fos;
if(dFile.length()==0) {
fos=new FileOutputStream(dFile);
}else {
fos=new FileOutputStream(dFile,true);
}
if((fis!=null)&&(fos!=null)) {
int temp=0;
while((temp=fis.read())!=(-1))
fos.write(temp);
}
fis.close();
fos.close();
System.out.println("文件复制成功");
}catch(IOException e) {
e.printStackTrace();
}
}
}