有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有ID自动增量生成器的java ObjectInputStream

我的视频。dat包含一些对象,这些对象由:V-1000*磁盘+AI存储

Video<ID; Name; Disk; $Fee>
Video<V1001; Avenger; 1; $12.0>
Video<V1002; 50 First Dates; 1; $13.0>
Video<V2001; Furious 7; 2; $18.0>

并将我的StartUp()加载到我的GUI。在启动时,我如何按照自动增量规则计数并继续添加视频对象

public void startUp() {
    int count = 0;
    try {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("videos.dat"))) {
            objects = (Vector) ois.readObject();
            for (Object v : objects) {
                System.out.println(v.toString());
                count++;
                fireObjectCreated(v);
            }
            ois.close();
        } catch (SecurityException ex) {
            Logger.getLogger(VideoManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(VideoManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Got " + count + " video objects");

    } catch (FileNotFoundException ex) {
        System.err.println("FileNotFoundException");
    } catch (IOException ex) {
        System.err.println("IOException");
    } catch (ClassNotFoundException ex) {
        System.err.println("ClassNotFoundException");
    }
}

我正在使用HashMap来存储和自动递增,但启动后()我的程序返回开始意味着它是从视频V1001开始的

public static int currentNumberOfStaticVideoID(int value) {
        if (listCountVideoID.containsKey(value)){
            System.out.println("Contain disk type " +value + " : " + listCountVideoID.containsKey(value));
            int id = listCountVideoID.get(value);
            System.out.println("Get current id: " + id);
            int newid = id + 1;
            listCountVideoID.put(value, newid);
            System.out.println("New id: " + newid);
            return newid;
        }
        int newKey = value*1000;
        listCountVideoID.put(value, newKey);
        return newKey;
    }
    public static Map<Integer, Integer> listCountVideoID = new HashMap<Integer,Integer>();

所以,任何人都可以帮助你找到一些目标:

 + load the data file >> load objects
 + continue store video with ID = "V" + 1000*disk + AutoIncre
 + if disk = 1 > continue from V1003
 + if disk = 2 > continue from V2002

共 (0) 个答案