我有一个主线程,我们从它启动两个后续线程,分别执行文件写入和文件读取操作。我有另一个名为 orderDetails 的类,具有属性及其 getter 和 setter。当前程序以这样一种方式运行,它将对象列表一次写入文件并读回。
问题:我需要将对象(cust1)的内容写入文件并同时从文件中读取。这必须对所有指定的对象(cust1,cust2)执行。提出解决此问题的可能方法。
主班
public class MainThread {
public static void main(String[] args) throws InterruptedException {
Thread1 td1=new Thread1();
Thread2 td2=new Thread2();
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t1.sleep(100);
t2.start();
}
}
orderDetails 类 - 一个普通的 pojo 类
public class orderDetails {
private String custName;
private double advancePaid;
private int mobileModel;
private boolean replacement;
private String problemFaced;
private boolean softwareRequirement;
public orderDetails(String custName, double advancePaid, int mobileModel,
boolean replacement, String problemFaced,
boolean softwareRequirement) {
super();
this.custName = custName;
this.advancePaid = advancePaid;
this.mobileModel = mobileModel;
this.replacement = replacement;
this.problemFaced = problemFaced;
this.softwareRequirement = softwareRequirement;
}
public boolean isSoftwareRequirement() {
return softwareRequirement;
}
public void setSoftwareRequirement(boolean softwareRequirement) {
this.softwareRequirement = softwareRequirement;
}
public int getMobileModel() {
return mobileModel;
}
public void setMobileModel(int mobileModel) {
this.mobileModel = mobileModel;
}
public String getProblemFaced() {
return problemFaced;
}
public void setProblemFaced(String problemFaced) {
this.problemFaced = problemFaced;
}
public boolean isReplacement() {
return replacement;
}
public void setReplacement(boolean replacement) {
this.replacement = replacement;
}
public double getAdvancePaid() {
return advancePaid;
}
public void setAdvancePaid(double advancePaid) {
this.advancePaid = advancePaid;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
}
文件写入——将属性写入文件
public class FileWriting implements Runnable {
orderDetails cust1=new orderDetails("vid",2000.00,2543,true,"display",false);
orderDetails cust2=new orderDetails("kesav",8000.00,7845,false,"battery",true);
ArrayList<orderDetails> orderArr=new ArrayList<orderDetails>();
orderDetails obj;
public void run() {
orderArr.add(cust1);
orderArr.add(cust2);
try {
for(orderDetails obj:orderArr)
{
fileOperations(obj);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public void fileOperations(orderDetails obj) throws IOException{
File f= new File("C:\\Users\\311518\\Desktop\\threadtest2.txt");
Calendar calNow = Calendar.getInstance();
Calendar orderDate;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
FileWriter fstream;
if(f.exists()){
//append mode
fstream = new FileWriter(f,true);
}
else
{
// to open a new file
f.createNewFile();
// write mode
fstream = new FileWriter(f,false);
}
BufferedWriter out = new BufferedWriter(fstream);
out.write(obj.getCustName()+" "+obj.getMobileModel()+" "+obj.getProblemFaced()+" "+obj.getAdvancePaid()+" "+obj.isReplacement()+" "+obj.isSoftwareRequirement());
double balanceAmt=obj.getAdvancePaid()-200;
out.write(" "+balanceAmt);
orderDate = (Calendar) calNow.clone();
out.write(" "+formatter.format(orderDate.getTime()));
orderDate.add(Calendar.DAY_OF_YEAR, + 10);
out.write(" "+formatter.format(orderDate.getTime()));
out.newLine();
out.close();
}
}
文件读取类——从文件中读取属性
public class FileReading implements Runnable{
File f= new File("C:\\Users\\311518\\Desktop\\threadtest2.txt");
public void run() {
try {
readingText();
System.out.println("Thanks for ur order");
} catch (IOException e) {
e.printStackTrace();
}
}
public void readingText() throws IOException{
String [] temp = null;
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s;
System.out.println("--------ORDER DETAILS------");
System.out.println("Name Model Adv Prob Replacement S/w Bal OrderDate Deliverdate ");
while((s = br.readLine()) != null) {
//display ordered details
temp = s.toString().split("\n");
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
}
}
}