Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

// Luu file text:

public boolean luuFileText(String path) {


try {
File file = new File(path);
BufferedWriter bw = Files.newBufferedWriter(file.toPath(),
StandardCharsets.UTF_8);
for (Book book : this.listBook) {
String line = new String();
line = book.getName() + ";" + book.getAuthor() + ";" +
book.getPrice() + ";" + book.covertDatetoString() + ";" + book.getNhaSX();
bw.write(line);
bw.newLine();
}
bw.close();
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}

// Doc File text:


public void docFileText(String path) {
try {
File file = new File(path);
BufferedReader br = Files.newBufferedReader(file.toPath(),
StandardCharsets.UTF_8);
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// Luu file Object:


public boolean luuFileObject(String path) {
try {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (Book book : this.listBook) {
oos.writeObject(book);
}
fos.close();
oos.close();
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}

//Doc file Object:


public void docFileObject(String path) {
try {
FileInputStream fis=new FileInputStream(path);
ObjectInputStream ois=new ObjectInputStream(fis);
while(true) {
Object obj=ois.readObject();
if(obj!=null) {
Book book1=(Book) obj;
this.listBook.add(book1);
}
else if(obj==null) break;
}
ois.close(); fis.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}

You might also like