I suggest that you read the file line by line and call method split to separate it into fields.
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Customer {
private String userName;
private String fullName;
private String password;
private String gender;
private String birthyear;
private String phone;
private String email;
private String address;
public Customer(String userName,
String fullName,
String password,
String gender,
String birthyear,
String phone,
String email,
String address) {
this.userName = userName;
this.fullName = fullName;
this.password = password;
this.gender = gender;
this.birthyear = birthyear;
this.phone = phone;
this.email = email;
this.address = address;
}
public static void main(String[] args) {
List<Customer> allCustomer = new ArrayList<>();
try (Scanner s = new Scanner(new File("Customer.txt"))) {
while (s.hasNextLine()) {
String line = s.nextLine();
String[] fields = line.split("\t");
String userName = fields[0];
String fullName = fields[1];
String password = fields[2];
String gender = fields[3];
String birthyear = fields[4];
String phone = fields[5];
String email = fields[6];
String address = fields[7];
allCustomer.add(new Customer(userName,
fullName,
password,
gender,
birthyear,
phone,
email,
address));
}
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Note that the above code uses try-with-resources to ensure that the file Customer.txt is closed.
Edit
Here is an alternative solution that uses records, streams, method references and NIO.2
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public record Customer(String userName,
String fullName,
String password,
String gender,
String birthyear,
String phone,
String email,
String address) {
private static final String DELIMITER = "\t";
private static Customer parseLine(String line) {
String[] fields = line.split(DELIMITER);
return new Customer(fields[0],
fields[1],
fields[2],
fields[3],
fields[4],
fields[5],
fields[6],
fields[7]);
}
public static void main(String[] args) {
try (Stream<String> lines = Files.lines(Paths.get("Customer.txt"))) {
List<Customer> allCustomer = lines.map(Customer::parseLine)
.collect(Collectors.toList());
allCustomer.forEach(System.out::println);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Running the above code produces the following output:
Customer[userName=yx, fullName=Yong, password=123, gender=Male, birthyear=2002, phone=999, email=jay@, address=NO234]
Customer[userName=paul, fullName=Paul Tan, password=123, gender=Male, birthyear=2002, phone=999, email=kkk, address=nnn]