HỎI Cho em hỏi em ghi đè dữ liêu lên file được mà đọc ra nó bi lỗi ạ

hahapho

New Member
6/12/20
2
0
1
123456789
Ghi file ạ
Java:
  FileOutputStream fos = new FileOutputStream("DS_PhieuNhap.txt",true);
            ObjectOutputStream oss = new ObjectOutputStream(fos);
            oss.writeObject(MangPhieuNhap);
            fos.close();
            oss.close();
========================================================================================================
Đoc file ạ
Java:
FileInputStream fin = null;
         try {
             fin=new FileInputStream("DS_PhieuNhap.txt");
         } catch (FileNotFoundException e) {
             System.out.print("\nKhong tim thay file");
         }
         ObjectInputStream oss=null;
         PhieuNhap[]c=null;
         try {
             oss= new ObjectInputStream(fin);
         } catch (IOException e) {
           //  Logger.getLogger(MangPhieuNhap.class.getName()).log(Level.SEVERE, null, e);
         }
         try {
             c=(PhieuNhap[]) oss.readObject();
         } catch (IOException | ClassNotFoundException e) {
             System.out.print("\nLoi doc file 1");
         }
         try {
            oss.close();
        } catch (IOException ex) {
            System.out.println("Rong 3");
        }
         System.out.print("\n==================================DOC FILE==================================\n");
         System.out.format("%5s%18s%25s%30s%25s\n", "Ma Phieu Nhap", "Ma Nhan Vien","Ma Nha Cung Cap", "Thoi Gian Nhap", "Don Gia");
        for(int i=0;i<c.length;i++){
            c.XuatPhieuNhap();
        }
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,022
1,335
113
Hi
First: read THIS for the ease of reading
Then, some questions:
1) is the object MangPhieuNhap an implementation of Interface Serializable? If NOT that is the error. Otherwise pls. show us the codes of MangPhieuNhap
Java:
public class MangPhieuNhap implements java.io.Serializable {
    ....
}
2) PhieuNhap[] is an array of whatever. Is MangPhieuNhap an Object array, too? If NOT that is the error. Otherwise pls. show us the codes of PhieuNhap[]
 
Sửa lần cuối:

hahapho

New Member
6/12/20
2
0
1
123456789
Java:
package PhieuNhap;



import DungChung.Check;

import DungChung.Mang;

import NhaCungCap.MangNhaCungCap;

import NhaCungCap.NhaCungCap;

import NhanVien.MangNhanVien;

import NhanVien.NhanVien;

import PhieuXuat.MangPhieuXuat;

import SanPham.DanhSachSanPham;

import SanPham.SanPham;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.i:confused:bjectInputStream;

import java.i:confused:bjectOutputStream;

import java.io.Serializable;

import java.util.Arrays;

import java.util.Date;

import java.util.Scanner;

import java.util.function.Supplier;

import java.util.logging.Level;

import java.util.logging.Logger;





public class MangPhieuNhap implements java.io.Serializable,Mang {



    private PhieuNhap[] MangPhieuNhap;

    private int soluongPhieuNhap;

    static Scanner sc = new Scanner(System.in);



    public MangPhieuNhap() {



    }

    @Override

    public void NhapMang() {

//========================================================================Doc file mang sinh vien===================================================///

        FileInputStream fin = null;

        try {

            fin = new FileInputStream("DS_NhanVien.txt");

        } catch (FileNotFoundException e) {

            System.out.print("\nKhong tim thay file");

        }

        ObjectInputStream oss = null;

        NhanVien[] DSNhanVien = null;

        try {

            oss = new ObjectInputStream(fin);

        } catch (IOException e) {

            Logger.getLogger(MangNhanVien.class.getName()).log(Level.SEVERE, null, e);

        }

        try {

            DSNhanVien = (NhanVien[]) oss.readObject();

        } catch (IOException | ClassNotFoundException e) {

            System.out.print("\nLoi doc file 1");

        }

        try {

            oss.close();

        } catch (IOException ex) {

            System.out.println("Rong 3");

        }

        System.out.print("\n==================================DOC FILE Mang nhan vien==================================");

        for(int j=0;j<DSNhanVien.length;j++){

            System.out.print("\nMannv"+DSNhanVien[j].getMaNhanVien());

        }

//====================================================Doc File Mang Nha Cung Cap=====================================================//

FileInputStream in=null;

         try {

             in=new FileInputStream("DS_NhaCungCap.txt");

         } catch (FileNotFoundException e) {

             System.out.print("\nKhong tim thay file");

         }

         ObjectInputStream on=null;

         NhaCungCap[]DSNhaCungCap=null;

         try {

             on= new ObjectInputStream(in);

         } catch (IOException e) {

             Logger.getLogger(MangNhaCungCap.class.getName()).log(Level.SEVERE, null, e);

         }

         try {

             DSNhaCungCap=(NhaCungCap[]) on.readObject();

         } catch (IOException | ClassNotFoundException e) {

             System.out.print("\nLoi doc file 1");

         }

         try {

            on.close();

        } catch (IOException ex) {

            System.out.println("Rong 3");

        }

         System.out.print("\n==================================DOC FILE Mang Nha Cung Cap==================================");

         System.out.format("\n%5s%18s%20s\n", "Ma Nha Cung Cap", "Ten Nha Cung Cap", "Quoc Gia");

         for(int i=0;i<DSNhaCungCap.length;i++){

             DSNhaCungCap[I].XuatNhaCungCap();

         }[/I]



//==================================================================================================================================//

        while (true) {

            System.out.print("\nNhap so luong Phieu Nhap: ");

            try {

                soluongPhieuNhap=sc.nextInt();

                MangPhieuNhap=new PhieuNhap[soluongPhieuNhap];

                //int gt=MangPhieuNhap.length;

               // MangPhieuNhap=Arrays.copyOf(MangPhieuNhap, MangPhieuNhap.length+1);

                sc.nextLine();

                for(int i=0;i<MangPhieuNhap.length;i++){

                    MangPhieuNhap=new PhieuNhap();

                    String a;

                    haha:  

                    while(true){

                       System.out.print("\nNhap ma Phieu Nhap:");

                        a=sc.nextLine();

                        for(int j=0;j<i;j++){

                            if(a.equals(MangPhieuNhap[j].getMaPhieuNhap())){

                                System.out.print("\nMa Phieu Nhap bi trung.....");

                                continue haha;

                            }

                        }

                        break haha;

                    }

                    MangPhieuNhap.setMaPhieuNhap(a);

                    String manv;

                    hihi:

                    do{

                        System.out.print("\nNhap ma Nhan vien cua Phieu Nhap:");

                        manv=sc.nextLine();

                        for(int k=0;k<DSNhanVien.length;k++){

                            if(manv.equals(DSNhanVien[k].getMaNhanVien())){

                                break hihi;

                            }

                          

                        }

                        System.out.print("\nMa Nhan vien ko ton tai...........");

                    }while(true);

                    MangPhieuNhap.setManv(manv);

                    Date date=java.util.Calendar.getInstance().getTime();

                    MangPhieuNhap.setThoiGianNhap(date);

                   String macc;

                    hihi:

                    do{

                        System.out.print("\nNhap ma Nha Cung Cap cua Phieu Nhap:");

                        macc=sc.nextLine();

                        for(int k=0;k<DSNhaCungCap.length;k++){

                            if(macc.equals(DSNhaCungCap[k].getMaNhaCungCap())){

                                break hihi;

                            }

                          

                        }

                        System.out.print("\nMa Nha Cung Cap ko ton tai...........");

                    }while(true);

                    MangPhieuNhap.setMancc(manv);

                    MangPhieuNhap.setDonGia(0);

                }

            } catch (Exception e) {

                System.out.print("\nLoi Nhap so luong phieu Nhap...Nhap lai:");

            }

            break;

        }

    }

    @Override

    public void XuatMang() {

        System.out.format("%5s%18s%25s%50s%55s\n", "Ma Phieu Nhap", "Ma Nhan Vien","Ma Nha Cung Cap     ", "Thoi Gian Nhap", "Don Gia");

        for (int i = 0; i < MangPhieuNhap.length; i++) {

            System.out.format("%5s | ", MangPhieuNhap.getMaPhieuNhap());

            System.out.format("%15s | ", MangPhieuNhap.getManv());

            System.out.format("%22s | ", MangPhieuNhap.getMancc());

            System.out.format("%38s |", MangPhieuNhap.getThoiGianNhap());

            System.out.format("%10s |\n", MangPhieuNhap.getDonGia());

        }

    }

   



    public void writefileMangPhieuNhap() throws IOException {

         

            FileOutputStream fos = new FileOutputStream("DS_PhieuNhap.txt",true);

            ObjectOutputStream oss = new ObjectOutputStream(fos);

            oss.writeObject(MangPhieuNhap);

            fos.close();

            oss.close();

    }

    public void readfileMangPhieuNhap()  {

        FileInputStream fin = null;

         try {

             fin=new FileInputStream("DS_PhieuNhap.txt");

         } catch (FileNotFoundException e) {

             System.out.print("\nKhong tim thay file");

         }

         ObjectInputStream oss=null;

         PhieuNhap[]c=null;

         try {

             oss= new ObjectInputStream(fin);

         } catch (IOException e) {

            Logger.getLogger(MangPhieuNhap.class.getName()).log(Level.SEVERE, (Supplier<String>) e);

         }

         try {

             c=(PhieuNhap[]) oss.readObject();

         } catch (IOException | ClassNotFoundException e) {

             System.out.print("\nLoi doc file 1");

         }

         try {

            oss.close();

        } catch (IOException ex) {

            System.out.println("Rong 3");

        }

         System.out.print("\n==================================DOC FILE==================================\n");

         System.out.format("%5s%18s%25s%30s%25s\n", "Ma Phieu Nhap", "Ma Nhan Vien","Ma Nha Cung Cap", "Thoi Gian Nhap", "Don Gia");

        for(int i=0;i<c.length;i++){

            c.XuatPhieuNhap();

        }

    }



    public static void main (String [] args) ném IOException {

        MangPhieuNhap a = new MangPhieuNhap ();

        a.NhapMang ();

        a.writefileMangPhieuNhap ();

        //a.docfileMangPhieuNhap();

         FileInputStream fin = null;

         thử {

             fin = new FileInputStream ("DS_PhieuNhap.txt");

         } catch (FileNotFoundException e) {

             System.out.print ("\ nKhong tim thay file");

         }

         ObjectInputStream us = null;

         PhieuNhap[]c=null;

         thử {

             us = new ObjectInputStream (tốt);

         } catch (IOException e) {

            Logger.getLogger (MangPhieuNhap.class.getName ()). Log (Level.SEVERE, null, e);

         } 

         thử {

             c=(PhieuNhap[]) oss.readObject();

         } catch (IOException | ClassNotFoundException e) {

             System.out.print ("\ nLoi doc file 1");

         }

        // thử {

            //oss.close();

       //} catch (IOException ex) {

            System.out.println ("Rong 3");

        //}

         System.out.print ("\ n ================================== TẬP TIN DOC ===== ============================= \ n "+ c.length);

         System.out.format("%5s%18s%25s%30s%25s\n", "Ma Phieu Nhap", "Ma Nhan Vien","Ma Nha Cung Cap", "Thoi Gian Nhap", "Don Gia");

        for (int i = 0; i <c.length; i ++) {

            c.XuatPhieuNhap();

        }



    }

}
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,022
1,335
113
You confuse the concept of Serializable Object and ObjectInput/OutputStream. It's impossible to change your codes because the design is wrong from the head down to the tail. Let me explain briefly the mentioned concept to you and then you could probably redesign your messy codes.

If an instantiated object needs to be saved (or frozen) it can be done only with object that is an implementation of the interface Serializable. The frozen state of an object contains all the initialized variables (e.g. String, array, list, primitives, etc.) and NOT the methods, NOR the dynamic state which should be "instantiated" or "initialized" by any internal method (e.g. loading array or String via Scanner). In your case you have done it with scanner and that means the state of your object is UNKNOWN when you save it with ObjectOutputStream. And when an object is read by ObjectInputStream it is expected that the object must be cast to the implemented Serializable object. In your case you declare PhieuNhap is an array with the name MangPhieuNhap (which is itself an implementation of Serializable Interface) and then you WRONGLY cast it to PhieuNhap (an array with the name MangPhieuNhap, NOT MangPhieuNhap object itself) and finally you make the next mistake to instantiate this array as a single object: MangPhieuNhap=new PhieuNhap().

Sorry, too much errors, bad design and messy implementation so that I cannot correct your codes without having to redesign the entire package of your project.
 
Sửa lần cuối: