Xuất dữ liệu ra file bị lỗi

Các bạn coi hộ mình bị lỗi gì với mọi người
Mình xuất dữ liệu ra file bằng ArrayList mà bị giống mã hoá - mình giải mã thì giống nó lưu quá trình chạy thì ( Hay lỗi á )
Dù mình đã implements Serializable
Không biết bị gì luôn mọi người à

Đây là code ghi file nè

         public  static class FileManager  {
          public static final String STUDENT_FILE_NAME = ("student.txt");
    public void write(List<Staff> studentList)throws FileNotFoundException {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            FileInputStream fw = new FileInputStream("student.txt");
            FileOutputStream rr = new FileOutputStream("student.txt");
            Writer out = new OutputStreamWriter(rr, "UTF8");
            fos = new FileOutputStream(new File(STUDENT_FILE_NAME));
            ObjectOutputStream ss = new ObjectOutputStream(rr);
            ss = new ObjectOutputStream(fos);
            ss.writeObject(studentList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeStream(fos);
            closeStream(oos);
        }
    }
    /**
     * read list student from file
     * 
     * @return list student
     */
    public List<Staff> read() {
        List<Staff> studentList = new ArrayList<>();
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(new File(STUDENT_FILE_NAME));
            ois = new ObjectInputStream(fis);
            studentList = (List<Staff>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeStream(fis);
            closeStream(ois);
        }
        return studentList;
    }
    /**
     * close input stream
     * 
     * @param is: input stream
     */
    private void closeStream(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * close output stream
     * 
     * @param os: output stream
     */
    private void closeStream(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Staff ReadFile(){
        Staff c=new Staff();
                try {
                    FileInputStream fis = new FileInputStream("student.txt");
                    ObjectInputStream ois = new ObjectInputStream(fis);
                   Object obj= ois.readObject();
                    c = (Staff) obj; 
                
                    System.out.println(c.toString());
                    ois.close();
                    System.out.println("Press Any Key To Continue...");
                    new java.util.Scanner(System.in).nextLine();
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                return c;
       }
     }
}

Còn đây là code nhân dữ liệu từ ArrayList khi nhập vào nè

public class ManageStaff implements Serializable{
    public static boolean isNumeric(String str) {
            return str.matches("-?\\d+(\\.\\d+)?");
        }
        ArrayList<Staff> listStaff= new ArrayList<>();
    ArrayList<Marketing> Mkt = new ArrayList<>();
    ArrayList<Manager> MaNa = new ArrayList<>();
    Staff staff=new Staff();
    Scanner input = new Scanner(System.in);
    ClearDisplay Cls=new ClearDisplay();
    private FileManager fileManager;
    List<Staff> ss;
    // lưu thông tin nhân viên
    
    public ManageStaff(){
            fileManager = new FileManager();
            ss = fileManager.read();
    }
    
    /// NHAP NHAN VIEN ( nhan viên )
   public void InputStaff(){
  
            boolean check= false;
            while(!check){
                System.out.print(" ");
                try{
                        
                            Cls.cls();
                            System.out.print("Nhập số lượng nhân viên cần nhập : ");
                            int Amount=input.nextInt();
                            FileWriter fw = new FileWriter("student.txt");
                            for (int i = 0; i < Amount; i++) {
                            Staff staff = new Staff("", "",0);
                            staff.inputStaff();
                            listStaff.add(staff); 
                            fileManager.write(listStaff);
                         
                            } 
                    }
                catch(Exception e){
                    System.out.println("Giá trị phải là số, hãy nhập lại...");
                    input.nextLine();
                    break;
                    }
                    break;
                }
               }

Bạn set UTF8 cho stream ghi vào file student.txt nhé.

Làm thế nào v bạn - không biết : :frowning:

Khi lưu file thì nhìn phần dưới gần nút Save có mục Encoding, nhần vào mũi tên trỏ xuống, chọn UTF8 rồi save lại là được

1 Like

Quan trọng là bạn có thể đọc ngược lại từ file ra được hay không thôi. Mình để ý là bạn dùng ObjectOutputStream.writeObject(), mà đây là class dạng byte stream chứ không phải char stream nên mở file ra sẽ có các kí tự đặc biệt như vậy.

2 Likes

bạn đang lưu nhị phân vào file student.txt nên “mắt thường” không đọc được, bạn thử cách của bác @tonghoangvu xem sao. Nếu đọc ngược lên được là OK

Cho mình hỏi sao gửi code lên được vậy ?

Vào VSC nhấn Ctrl + A rồi đến Ctrl + C sau đó vào DNH nhấn Ctrl + V. :smiling_imp:
Không biết mấy phím này sao xứng đáng là dân IT nhỉ. :woman_facepalming::man_facepalming:

1 Like

Ý là gửi code cho gọn đấy á
theo từng khúc :slight_smile:

@AzDev Làm thế cũng vẫn bị lỗi b à

@anon71189590 Làm thế cũng vẫn bị lỗi b à

  1. Mình đã sửa bài cho bạn, có nút chỉnh sửa bài đấy, nhấn vào xem mình sửa thế nào và tìm kiếm với từ khóa markdown để biết cách đăng bài cho đẹp.
  2. Về vấn đề của bạn: Bạn đang cố mở tập tin dữ liệu nhị phân để xem dạng văn bản.
    Bạn đã dùng ObjectStreamSerializable có nghĩa dữ liệu được tuần tự hóa sang dạng nhị phân.
  3. Giải quyết vấn đề: Nếu muốn lưu dạng văn bản, thì tự bạn lưu trữ từng thuộc tính, đừng có nhờ vả vào ObjectStreamSerializable.
6 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?