package controller;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FileUtils;
import model.Category;
public class FileCategory {
List<Category> categories = new CategoryController().getCategories();// lay data tu controller
// add(id: 1 - name: 1)
// add(id: 2 - name: 2)
// add(id: 3 - name: 3)
// Tao file
public void writeCategoryFile() throws IOException {
File file = new File("file\\category.txt");
if (categories.isEmpty()) {
System.out.println("Danh sach category rong");
} else
FileUtils.writeLines(file, categories);// chuyen object category trong categories thanh string va luu vao
// trong file
}
public void readFileCategory() throws IOException {
File file = new File("file\\category.txt");
if (file.exists()) {
List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);// doc du lieu tu file
lines.forEach(System.out::println);// in du lieu ra
// (id: 1 - name: 1)
// (id: 2 - name: 2)
// (id: 3 - name: 3)
List<Object> list = Arrays.asList(lines.toArray());//chuyen list string thanh list object
list.forEach(System.out::println);//in data
//lam sao de chuyen cai object nay sang thanh category
} else {
System.out.print("File nay khong ton tai");
}
}
Làm sao để có thể chuyển list<obejct>
thành list<category>