当我尝试将数组列表写入文件时,它第一次正常工作。但每次重新启动程序时,第一次单击按钮写入文件时,它会复制列表。
例如,我第一次运行程序并将以下对象添加到数组列表中,然后写入文件,输出文件为:
Aaaaaa,aaaaaaa
如果我重新启动程序,然后再做同样的事情,输出是:
Bbbbbb,bbbbbbb
Aaaaaa,aaaaaaa
Aaaaaa,aaaaaaa
第三次:
Cccccc,cccccc
bbbbbb,bbbbbb
aaaaa,aaaa
aaaaa,aaaa
bbbbbb,bbbbbb
aaaaa,aaaa
aaaaa,aaaa
等等。到目前为止,我还没有找到导致这种情况的原因。
这是fxml控制器:
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GUImembersController implements Initializable
{
private Member member;
private MemberList memberList = new MemberList();
private String filename = "MemberList.txt";
private MemberFile memberFile = new MemberFile(filename);
ObservableList<Member> members = FXCollections.observableArrayList();
@FXML private BorderPane memberPage = new BorderPane();
@FXML private Button btnAddMember,btnClearTextFields;
@FXML private TextField tfEnterMemberName,tfEnterMemberEmail;
@FXML private TableView<Member> memberTable;
@FXML private TableColumn<Member, String> tcMemberName,tcMemberEmail;
@Override
public void initialize(URL location, ResourceBundle resources)
{
memberPage.setMaxHeight(Double.MAX_VALUE);
memberPage.setMaxWidth(Double.MAX_VALUE);
tcMemberName.setCellValueFactory(new PropertyValueFactory<Member, String>("name"));
tcMemberEmail.setCellValueFactory(new PropertyValueFactory<Member, String>("email"));
try {
memberTable.setItems(getList());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
public ObservableList<Member> getList() throws FileNotFoundException, ParseException
{
MemberList memberLi = memberFile.readMemberTextFile();
for (int i = 0; i < memberLi.size(); i++)
{
members.add(new Member(memberLi.getMember(i).getName(), memberLi.getMember(i).getEmail()));
}
return members;
}
@FXML
void addMember(ActionEvent event) throws ParseException, CloneNotSupportedException, IOException
{
String memberName = tfEnterMemberName.getText();
String memberEmail = tfEnterMemberEmail.getText();
member = new Member(memberName,memberEmail);
memberList.addMemberToList(member);
memberFile.writeMemberTextFile(memberList);
clearTextFields(event);
}
@FXML
void clearTextFields(ActionEvent event)
{
tfEnterMemberName.setText("");
tfEnterMemberEmail.setText("");
}
}
这是我处理文件写入和读取的类:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;
public class MemberFile {
private int sw = 0;
private File file;
private String filename = "MemberList.txt";
private MemberList currentMemberList = new MemberList();
private MemberList currentMembers = new MemberList();;
private MemberList memberList = new MemberList();
public MemberFile(String filename) {
setFile(filename);
}
public void setFile(String filename) {
file = new File(filename);
}
public File getFile() {
return this.file;
}
public MemberList readMemberTextFile() throws FileNotFoundException, ParseException {
Scanner input = null;
try {
input = new Scanner(file);
int lineNumber = 0;
while (input.hasNext()) {
String line = input.nextLine();
String[] lineToken = line.split(",");
lineNumber++;
String memberName = lineToken[0].trim();
String memberEmail = lineToken[1].trim();
Member member = new Member(memberName, memberEmail);
currentMemberList.addMemberToList(member);
}
return currentMemberList;
}
finally {
input.close();
}
}
public void writeMemberTextFile(MemberList memberList) throws ParseException, FileNotFoundException, CloneNotSupportedException
{
PrintWriter output = null;
switch(sw)
{
case 0:
System.out.println("Reading from file");
currentMembers = readMemberTextFile();
for(int i = 0; i < currentMembers.size(); i++)
{
memberList.addMemberToList(currentMembers.getMember(i));
}
currentMembers.clearMemberList();
System.out.println(memberList.size());
sw = 1;
case 1:
System.out.println("Writing to file");
System.out.println(memberList.size());
output = new PrintWriter(file);
for (int i = 0; i < memberList.size(); i++)
{
output.println(memberList.getMember(i).getName() + "," + memberList.getMember(i).getEmail());
}
output.flush();
output.close();
break;
}
}
}