Reputation: 13
I am trying to write a program that creates a kind of database in a file. Its base functions are Read all records and Append a new record to file, but every time I try to append something nothing happens.
Here is my code:
public class SavingInFile {
FileWriter example=new FileWriter("template.txt",true);
FileReader example2=new FileReader("template.txt");
BufferedWriter setNewRecord=new BufferedWriter(example);
BufferedReader readTheRecords=new BufferedReader(example2);
public SavingInFile() throws IOException {
}
void safeNewData(PersonRecord anotherPersonRecord) throws IOException {
setNewRecord=new BufferedWriter(example);
setNewRecord.append(anotherPersonRecord.addNewRecord());
}
ArrayList<PersonRecord> getDataFromFile() throws IOException {
setNewRecord=new BufferedWriter(example);
readTheRecords=new BufferedReader(example2);
ArrayList<PersonRecord> valuesKeeper = new ArrayList<>();
System.out.println(readTheRecords.readLine());
while(readTheRecords.readLine()!=null) {
if(readTheRecords.readLine()!=null) {
String[] tempArray = readTheRecords.readLine().split(";");
PersonRecord tempRecord = new PersonRecord();
tempRecord.name = tempArray[0];
tempRecord.age = Integer.parseInt(tempArray[1]);
tempRecord.surname = tempArray[2];
tempRecord.salary = Integer.parseInt(tempArray[3]);
}
}
this.setNewRecord.close();
return valuesKeeper;
}
public void displayArrayList() throws IOException {
ArrayList<PersonRecord> example=this.getDataFromFile();
Iterator<PersonRecord> iterateValues=example.iterator();
while(iterateValues.hasNext()) {
System.out.println(iterateValues.next().name+ " "+iterateValues.next().surname+ " "+iterateValues.next().age+ " " + iterateValues.next().salary+ " ");
}
}
public void closeBuffers() throws IOException {
this.readTheRecords.close();
}
}
This is a Person Class, which contains dataFields
public class PersonRecord {
int age;
double salary;
String name;
String surname;
public PersonRecord() {
}
public PersonRecord(int age, double salary, String name, String surname) {
this.age = age;
this.salary = salary;
this.name = name;
this.surname = surname;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public void setAge(int age) {
this.age = age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public void setSurname(String surname) {
this.surname = surname;
}
String addNewRecord() {
return this.name+";"+this.surname+";"+String.valueOf(this.age)+";"+String.valueOf(this.salary);
}
}
and here is my main:
public class Main {
public static void main(String[] args) throws IOException {
SavingInFile bufferToFile = new SavingInFile();
ArrayList<PersonRecord> keepRecords=bufferToFile.getDataFromFile();
PersonRecord x = new PersonRecord(10,13,"34","45");
bufferToFile.saveNewData(x);
ArrayList<PersonRecord> example=bufferToFile.getDataFromFile();
bufferToFile.displayArrayList();
bufferToFile.closeBuffers();
}
}
Any suggestions about what is wrong there?
Upvotes: 1
Views: 123
Reputation: 18582
Your code has a lot of "bad smells" places. Just go through the following resources:
So following is the rewritten solution
Record class:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class PersonRecord {
String name;
String surname;
int age;
double salary;
String addNewRecord() {
return String.format("%s;%s;%s;%s\n", this.name, this.surname, this.age, this.salary);
}
}
Turn separate utility class for processing operation with the file for persons:
final class PersonOperations {
private Personoperations() {
}
public static void appendToFile(String fileName, List<PersonRecord> persons) throws IOException {
@Cleanup
BufferedWriter buffWriter = new BufferedWriter(new FileWriter(fileName, true));
for (PersonRecord person : persons) {
buffWriter.append(person.addNewRecord());
}
}
public static List<PersonRecord> readFile(String fileName) throws IOException {
@Cleanup
BufferedReader buffReader = new BufferedReader(new FileReader(fileName));
List<PersonRecord> persons = buffReader.lines()
.filter(Objects::nonNull)
.map(str -> {
String[] temp = str.split(";");
return PersonRecord.builder()
.name(temp[0])
.surname(temp[1])
.age(Integer.parseInt(temp[2]))
.salary(Double.parseDouble(temp[3]))
.build();
})
.collect(Collectors.toList());
System.out.println("persons from file: " + persons);
return persons;
}
public static void displayPersons(List<PersonRecord> persons) {
for (PersonRecord person : persons) {
System.out.println(person);
}
}
}
Final demo code will look like:
public class FileDemo {
public static void main(String[] args) throws IOException {
String fileName = "template.txt";
List<PersonRecord> records = PersonOperations.readFile(fileName);
PersonOperations.displayPersons(records);
PersonRecord personRecord = new PersonRecord("Bob", "Dilan", 34, 4500);
PersonOperations.appendToFile(fileName, List.of(personRecord));
List<PersonRecord> persons = PersonOperations.readFile(fileName);
PersonOperations.displayPersons(persons);
}
}
Also, if we don't need any operation with a result from the file, we could add static import and write:
displayPersons(readFile(fileName));
Console output:
persons from file: [PersonRecord(name=Garry, surname=Ford, age=45, salary=6000.0)]
PersonRecord(name=Garry, surname=Ford, age=45, salary=6000.0)
persons from file: [PersonRecord(name=Garry, surname=Ford, age=45, salary=6000.0), PersonRecord(name=Bob, surname=Dilan, age=34, salary=4500.0)]
PersonRecord(name=Garry, surname=Ford, age=45, salary=6000.0)
PersonRecord(name=Bob, surname=Dilan, age=34, salary=4500.0)
template.txt
file before execution:
Garry;Ford;45;6000
template.txt
after execution:
Garry;Ford;45;6000
Bob;Dilan;34;4500.0
Also, I used Lombok framework annotations there. You can keep native getters and setters as well. And closing resources after reading it (with Autocloseable or with finally block).
As alternatives for saving to txt
file you could consider:
Upvotes: 1
Reputation: 427
First of all, when you use FileWrite and BufferWriter you have to make sure to close this them or to use try with resources and below will solve your issue: And I strongly suggest rewriting everything in a cleaner way.
void safeNewData(PersonRecord anotherPersonRecord) throws IOException {
try (FileWriter example = new FileWriter("template.txt", true); BufferedWriter newRec = new BufferedWriter(example)){
newRec.write(anotherPersonRecord.addNewRecord());
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0