Anh ơi cho em hỏi em write 1 ArrayList xuống được nhưng khi đọc lên thì lại đọc không được cũng không hiện ra lỗi anh xem giúp em bị lỗi gì với ạ.
public String SaveUser(ArrayList<User> user) throws IOException {
File dir = null;
File f = null;
FileOutputStream fileOut = null;
String className = this.getClass().getName().replace('.', '/');
String classJar = this.getClass().getResource("/" + className + ".class").toString();
/* tham khảo từ https://www.rgagnon.com/javadetails/java-0391.html */
if (classJar.startsWith("jar:")) {
System.out.println("*** running from jar!");
String jarPath;
try {
/* tham khảo từ https://coderanch.com/t/669663/java/load-write-file-getClass-getResource */
jarPath = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
String completePath = jarPath.substring(0, jarPath.lastIndexOf("/")) + File.separator + "data" + File.separator + filename;
f = new File(completePath);
if (!f.exists() && !f.createNewFile()) {
System.out.println("File doesn't exist, and creating file with path: " + completePath + " failed. ");
} else {
System.out.println("Input data exists, or file with path " + completePath + " created successfully. ");
}
fileOut = new FileOutputStream(completePath, true);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(ParseData.class.getName()).log(Level.SEVERE, null, ex);
}catch (Exception e) {
Logger.getLogger(ParseData.class.getName()).log(Level.SEVERE, null, e);
}
} else {
try {
dir = new File("src/data");
f = new File(dir, filename);
if (!f.exists()) {
f = new File(dir, filename);
}
fileOut = new FileOutputStream(f.getAbsolutePath(), true);
} catch (FileNotFoundException ex) {
Logger.getLogger(ParseData.class.getName()).log(Level.SEVERE, null, ex);
}
}
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(user);
out.close();
fileOut.close();
return "Register_Success";
}
đây là phần Write File
public ArrayList<User> LoadUser() throws ClassNotFoundException{
File dir = null;
File f = null;
FileInputStream fi = null;
ObjectInputStream oi = null;
ArrayList<User> list = new ArrayList<User>();
try{
String className = this.getClass().getName().replace('.', '/');
String classJar = this.getClass().getResource("/" + className + ".class").toString(); /* tham khảo từ https://www.rgagnon.com/javadetails/java-0391.html */
if (classJar.startsWith("jar:")) {
System.out.println("*** running from jar!");
String jarPath = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8"); /* tham khảo từ https://coderanch.com/t/669663/java/load-write-file-getClass-getResource */
String completePath = jarPath.substring(0, jarPath.lastIndexOf("/")) + File.separator + "data" + File.separator + filename;
f = new File(completePath);
try {
if (!f.exists() && !f.createNewFile()) {
System.out.println("File doesn't exist, and creating file with path: " + completePath + " failed. ");
}else{
System.out.println("Input data exists, or file with path " + completePath + " created successfully. ");
}
fi = new FileInputStream(completePath);
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
dir = new File("src/data");
f = new File(dir, filename);
if (f.exists()) {
fi = new FileInputStream(f.getAbsolutePath());
}
}
if(fi != null){
oi = new ObjectInputStream(fi);
list = (ArrayList<User>) oi.readObject();
}
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
if(oi != null){
oi.close();
}
if(fi != null){
fi.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return list;
}
đây là phần Read File