大家好,又见面了,我是你们的朋友全栈君。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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 arlist=new ArrayList();
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 AscNumber(ArrayList arlist){
Collections.sort(arlist, new Comparator() {
public int compare(Worker o1, Worker o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.getNumber(),o2.getNumber());
}
});
return arlist;
}
public ArrayList showAll(){
return arlist;
}
public ArrayList showByNumber(String input){
ArrayList anslist=new ArrayList();
for(Worker w:arlist) {
if(w.getNumber().contains(input)) {
anslist.add(w);
}
}
return anslist;
}
public ArrayList showByName(String input){
ArrayList anslist=new ArrayList();
for(Worker w:arlist) {
if(w.getName().contains(input)) {
anslist.add(w);
}
}
return anslist;
}
public void setFileData(String filepath,ArrayList 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 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 anslist=new ArrayList();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(filepath));
anslist=(ArrayList)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
//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();
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156497.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...