java吧 关注:1,240,618贴子:12,712,727
  • 3回复贴,共1
package worker1;
import java.io.Serializable;
public class Worker implements Serializable{
private String number;
private String name;
private String classes;
private String score;
public Worker() {
}
public Worker(String number,String name,String classes,String score) {
this.number=number;
this.name=name;
this.classes=classes;
this.score=score;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String toString() {
return this.number+" "+this.name+" "+this.classes+" "+this.score;
}
}


1楼2019-12-23 21:55回复
    package worker2;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.text.Collator;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Locale;
    import javax.swing.JOptionPane;
    import worker1.Worker;
    public class Workertwo {
    ArrayList<Worker> arlist=new ArrayList<Worker>();
    public void getFileData(String filepath) {
    FileInputStream fis;
    try {
    fis=new FileInputStream(filepath);
    InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
    BufferedReader br=new BufferedReader(isr);//缓冲区读取
    String line=null;
    while((line=br.readLine())!=null) {
    String[] flds=line.split("\\ +");
    Worker wr=new Worker(flds[0],flds[1],flds[2],flds[3]);
    if(flds[0].contains("学号")) {
    }else {
    arlist.add(wr);
    }
    }
    br.close();
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "File not found!/Charset not supported!");
    //e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "IO Error!");
    //e.printStackTrace();
    }
    }
    public ArrayList<Worker> AscNumber(ArrayList<Worker> arlist){
    Collections.sort(arlist, new Comparator<Worker>() {
    public int compare(Worker o1, Worker o2) {
    return Collator.getInstance(Locale.CHINESE).compare(o1.getNumber(),o2.getNumber());
    }
    });
    return arlist;
    }
    public ArrayList<Worker> showAll(){
    return arlist;
    }
    public ArrayList<Worker> showByNumber(String input){
    ArrayList<Worker> anslist=new ArrayList<Worker>();
    for(Worker w:arlist) {
    if(w.getNumber().contains(input)) {
    anslist.add(w);
    }
    }
    return anslist;
    }
    public ArrayList<Worker> showByName(String input){
    ArrayList<Worker> anslist=new ArrayList<Worker>();
    for(Worker w:arlist) {
    if(w.getName().contains(input)) {
    anslist.add(w);
    }
    }
    return anslist;
    }
    public void setFileData(String filepath,ArrayList<Worker> relist) {
    FileOutputStream fos;
    try {
    fos = new FileOutputStream(filepath);
    OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
    BufferedWriter bw=new BufferedWriter(osw);
    bw.write("学号 姓名 班级 java成绩");
    bw.newLine();
    for(Worker w:relist) {
    bw.write(w.toString());
    bw.newLine();
    }
    bw.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "File not found!");
    //e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "Charset not supported!");
    //e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "IO Error!");
    //e.printStackTrace();
    }
    }
    //异常处理
    public void WorkerthreeFileData(String filepath,ArrayList<Worker> relist) {
    //sserialization 序列化
    try {
    ObjectOutputStream ous=new ObjectOutputStream(new FileOutputStream(filepath));
    ous.writeObject(relist);
    ous.close();
    ous.writeObject(relist.size());
    for(Worker w:relist) {
    ous.writeObject(w.toString());
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    //序列化查询
    @SuppressWarnings("unchecked")
    public void getFileDataFromSeria(String filepath) {
    ArrayList<Worker> anslist=new ArrayList<Worker>();
    try {
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(filepath));
    anslist=(ArrayList<Worker>)ois.readObject();
    for(Worker i:anslist) {
    System.out.println(i.toString());
    }
    //int size=(int) ois.readObject();
    ////String w=(String) ois.readObject();
    //for(int i=0;i<size;i++) {
    //String w=(String) ois.readObject();
    //System.out.println(w);
    //}
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }


    2楼2019-12-23 21:56
    回复
      package worker3;
      import java.io.File;
      import java.io.IOException;
      import java.text.SimpleDateFormat;
      import java.util.ArrayList;
      import java.util.Date;
      import worker1.Worker;
      import worker2.Workertwo;
      public class Workerthree extends Thread{
      ArrayList<Worker> relist;
      public Workerthree() {
      }
      public Workerthree(String filename,ArrayList<Worker> relist) {
      super(filename);//线程赋名字
      this.relist=relist;
      }
      public void run() {
      Workertwo wu=new Workertwo();
      File fileCreateByPoint =new File("D:\\log");//存储路径
      if(!fileCreateByPoint.exists()) {//创建log文件夹,如果不存在则创建
      fileCreateByPoint.mkdirs();
      }
      try {
      //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//设置日期格式,存储log文件
      //df.format(new Date())
      File fileCreate =new File(fileCreateByPoint.getCanonicalPath()+"\\"+this.getName()+".txt");
      String filepath=fileCreate.getCanonicalPath();
      wu.WorkerthreeFileData(filepath,relist);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }


      3楼2019-12-23 21:56
      回复
        ?问题


        IP属地:湖南来自Android客户端4楼2019-12-23 22:23
        回复