gpt4 book ai didi

java - 在java中操作文本文件

转载 作者:行者123 更新时间:2023-11-30 04:41:26 25 4
gpt4 key购买 nike

我想显示存储在文本文件中的所有客户,但一个客户存储在许多文本文件中,并且他多次显示。如何消除这些重复客户?我什至尝试过使用 HashSet 但没有成功。这是我的代码。

public class ReadFile {

private int countCustomer = 0;
private File path;
private List<Customer> customers = new ArrayList<Customer>();

public ReadFile(File file_path) {

path = file_path;

}

public String[] openFile() throws IOException {

FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
textData[i] = br.readLine();

}

br.close();
return textData;
}

public void arrayCustomer(){

try{
String[] array = openFile();
for (int j=0; j< array.length;j++)
{
String str = array[j];
String[] temp;
String delimiter = " ";

temp = str.split(delimiter);

String category = temp[0];
double balance = Double.parseDouble(temp[1]);
int moveNumber = Integer.parseInt(temp[2]);
int accountNumber = Integer.parseInt(temp[3]);

if (!isIn(new Customer(category, balance, moveNumber, accountNumber)))
{
customers.add(new Customer(category, balance, moveNumber, accountNumber));
countCustomer++;
}
}
}
catch(IOException e){
e.printStackTrace();
}

}

public boolean isIn(Customer customer){

for (int i = 0; i < customers.size(); i++){
if (customers.get(i).getAccountNumber() == customer.getAccountNumber())
{
return true;
}
}
return false;
}

public void listCustomers() {
for (Customer customer : customers) {
System.out.print("Category: " + customer.getCategory());
System.out.print(" Balance: " + customer.getBalance());
System.out.print(" Moves: " + customer.getMoveNumber());
System.out.println(" Account number: " + customer.getAccountNumber());
}
}


public int readLines() throws IOException {

FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);

int numberOfLines = 0;
String aLine;

while((aLine = br.readLine()) != null)
{
numberOfLines++;
}
br.close();
return numberOfLines;
}



// The class with the main method

public class FileList {

public static Scanner scan;
public static ReadFile f;
public static final File folder = new File("C:/My_dir");

public static void main(String[] args) {

Logger logger = Logger.getLogger("textfiles");
scan = new Scanner(System.in);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++)
{
File fileEntry = listOfFiles[i];

if (!fileEntry.isDirectory())
{
try{
f = new ReadFile(fileEntry);
f.openFile();
f.arrayCustomer();
f.listCustomers();
}
catch(IOException e)
{
e.printStackTrace();
}
}
else
{
logger.log(Level.INFO, "The directory is empty!");
}
}

最佳答案

如果您的客户定义正确,Set应该可以解决问题。您需要确保您的 Customer 具有 equals()hashCode() 对象,它们对同一组字段进行操作并且可以正确地进行操作。确定两个客户是否“相同”或不符合您的业务标准的定义。

关于java - 在java中操作文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12193807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com