Java 要进行文件传输的对象数组

Java 要进行文件传输的对象数组,java,Java,我需要将存储在数组中的数据传输到一个文件中,在本例中,我将该文件创建为emplo.dat。我找不到做这件事的正确方法。使用readObject传输对象不起作用,因为它是一个对象数组 import java.io.*; import employee.*; public class ClearTechSolutions { public static void main(String[] args) { ContractEmployee[] con = new Contr

我需要将存储在数组中的数据传输到一个文件中,在本例中,我将该文件创建为emplo.dat。我找不到做这件事的正确方法。使用readObject传输对象不起作用,因为它是一个对象数组

import java.io.*;
import employee.*;

public class ClearTechSolutions {
    public static void main(String[] args) {
        ContractEmployee[] con = new ContractEmployee[3];
        PermanentEmployee[] per = new PermanentEmployee[3];

        for(int i=0;i<3;i++) {
            con[i] = new ContractEmployee();
            per[i] = new PermanentEmployee();
        }

        con[0].setData( "C001", "Mohan", "E-32 M.G Marg", "30/Jun/1974", 0.0f, 1000.0f, 0.0f,     5000.0f, 20, 7);
        con[1].setData( "C002", "Steve", "A-32 M.G. Marg", "15/Oct/1981", 0.0f, 1500.0f, 0.0f,     7500.0f, 22, 3);
        con[2].setData( "C003", "Mary", "A-31 Rohini", "15/Dec/1979", 0.0f, 2500.0f, 0.0f, 10000.0f,     18, 5);
        per[0].setData( "E001", "Bob", "E-12 Lajpat Nagar", "01/Feb/1974", 0.0f, 20000.0f, 0.0f,     800000.0f, 7, 28);
        per[1].setData( "E002", "Kevin", "E-15 Mandir Marg", "01/Apr/1990", 0.0f, 25000.0f, 0.0f,     1000000.0f, 6, 26);
        per[2].setData( "E003", "Mohan", "E-15 Mandir marg", "31/July/1984",0.0f, 10000.0f, 0.0f,     400000.0f,9,30);
        con[0].calc(1000.0f);
        con[1].calc(1500.0f);
        con[2].calc(2500.0f);
        per[0].calc(20000.0f);
        per[1].calc(25000.0f);
        per[2].calc(10000.0f);

        try {
            File file = new File("emplo.dat");
            file.createNewFile();
            FileOutputStream fi = new FileOutputStream(file);
            ObjectOutputStream obj = new ObjectOutputStream(fi);

            for(int i=0;i<3;i++) {
                System.out.println("\n");
                System.out.println("The details for Contract Employee "+(i+1)+" are:");
                con[i].print();
            }
            for(int i=0;i<3;i++) {
                System.out.println("\n");
                System.out.println("The details for Permanent Employee "+(i+1)+" are:");
                per[i].print();
            }
        }
        catch(IOException e) {}
    }
}
import java.io.*;
输入雇员*;
公共类ClearTechSolutions{
公共静态void main(字符串[]args){
ContractEmployee[]con=新的ContractEmployee[3];
永久员工[]per=新的永久员工[3];

对于ethrbunny建议的(inti=0;i),您可能应该了解有关序列化的更多信息

对于这个问题,你应该

  • 确保ContractEmployee类和PermanentEmployee类 实现可序列化接口
  • 使用ObjectOutputStream“obj” 创建的实例,调用
    obj.writeObject(con)
    obj.writeObject(per)
    写入数组中所需的数据 进入文件
  • 请注意,您向文件中写入了一个对象数组,而不是一个接一个地写入单个对象。因此,在反序列化时,还需要返回一个对象数组

        import java.io.*;
        import employee.*;
    
        public class ClearTechSolutions {
            public static void main(String[] args) {
                ContractEmployee[] con = new ContractEmployee[3];
                PermanentEmployee[] per = new PermanentEmployee[3];
    
                for(int i=0;i<3;i++) {
                    con[i] = new ContractEmployee();
                    per[i] = new PermanentEmployee();
                }
    
                con[0].setData( "C001", "Mohan", "E-32 M.G Marg", "30/Jun/1974", 0.0f, 1000.0f, 0.0f,     5000.0f, 20, 7);
                con[1].setData( "C002", "Steve", "A-32 M.G. Marg", "15/Oct/1981", 0.0f, 1500.0f, 0.0f,     7500.0f, 22, 3);
                con[2].setData( "C003", "Mary", "A-31 Rohini", "15/Dec/1979", 0.0f, 2500.0f, 0.0f, 10000.0f,     18, 5);
                per[0].setData( "E001", "Bob", "E-12 Lajpat Nagar", "01/Feb/1974", 0.0f, 20000.0f, 0.0f,     800000.0f, 7, 28);
                per[1].setData( "E002", "Kevin", "E-15 Mandir Marg", "01/Apr/1990", 0.0f, 25000.0f, 0.0f,     1000000.0f, 6, 26);
                per[2].setData( "E003", "Mohan", "E-15 Mandir marg", "31/July/1984",0.0f, 10000.0f, 0.0f,     400000.0f,9,30);
                con[0].calc(1000.0f);
                con[1].calc(1500.0f);
                con[2].calc(2500.0f);
                per[0].calc(20000.0f);
                per[1].calc(25000.0f);
                per[2].calc(10000.0f);
    
                try {
                    File file = new File("emplo.dat");
                    file.createNewFile();
                    FileOutputStream fi = new FileOutputStream(file);
                    ObjectOutputStream obj = new ObjectOutputStream(fi);
    
                    for(int i=0;i<3;i++) {
                        System.out.println("\n");
                        System.out.println("The details for Contract Employee "+(i+1)+" are:");
                        con[i].print();
    obj.writeObject(con[i]);                <-- this will write your object to file connected to file output stream.
                    }
                    for(int i=0;i<3;i++) {
                        System.out.println("\n");
                        System.out.println("The details for Permanent Employee "+(i+1)+" are:");
                        per[i].print();
    obj.writeObj(per[i]);
                    }
                }
                catch(IOException e) {}
            }
        }
    

    这里abc是您要检索其对象的类名。fin in file input stream object。

    原语数组也是一个对象。
    传输到文件
    意味着写入到文件?我想这就是它的意思。问题在于该语言。哦,忘了提一下。您的类必须实现可序列化才能使用序列化概念。
    ObjectInputStream ois=new ObjectInputStream(fin);
     abc temp; 
      while((temp=(abc)ois.readObject()))
    {
      System.out.println(temp);
      }