Java <identifier> 需要 writeObject()

标签 java compiler-errors

对于类作业,我需要将对象写入文件。我们的教授给了我们一段代码来完成这个任务,但显然它是错误的,因为我收到了一个错误。这是我的代码。

class InvMaintenance {

    //create an OutputStream to write data to a file
    FileOutputStream fos = new FileOutputStream(inven.dat);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    final long MAX_SIZE = 100; //constant for array length
    Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object
    oos.writeObject(cInventory); //write initial Inventory to file  
    public static void main(String[] args) {
        //Output options
        /*  Inventory Maintenance
            1) Add Item
            2) Remove Item
            3) Sell Item
            4) Receive Item
            5) Display Inventory
            6) Quit
            Please Select NUMBER: */

        //switch on options
        //call appropriate method

        oos.writeObject(cInventory);
        oos.close();
    }
}

我的错误发生在 oos.writeObject(cInventory);

行上
Item.java:150: <identifier> expected
    oos.writeObject(cInventory);            //write initial Inventory to file   
                   ^
Item.java:150: <identifier> expected
    oos.writeObject(cInventory);            //write initial Inventory to file   
                              ^
2 errors

是的,出于某种原因,它说这是两个独立的错误......完全相同。

任何调试帮助将不胜感激。怎么了?

最佳答案

必须在主方法中

//create an OutputStream to write data to a file
FileOutputStream fos = new FileOutputStream(inven.dat);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);

Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object

更改的代码:

class InvMaintenance {

    final static long MAX_SIZE = 100;          //constant for array length

    public static void main(String[] args)
    {
        //Output options
            /*  Inventory Maintenance
                1) Add Item
                2) Remove Item
                3) Sell Item
                4) Receive Item
                5) Display Inventory
                6) Quit
                Please Select NUMBER: */

        //switch on options
        //call appropriate method

        //create an OutputStream to write data to a file
        FileOutputStream fos = new FileOutputStream(inven.dat);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object

        oos.writeObject(cInventory);
        oos.close();
    }   

}

提示:将常量更改为final static long ..如果您使用static,则值会在编译时复制

提示2:处理异常..

关于Java <identifier> 需要 writeObject(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960277/

相关文章:

java - 单击展开 ListItem 到新 View

Java:将 Excel 中的数据存储在 ArrayList 的 ArrayList 中时出现问题

java - 如何创建枚举类型的映射?

消费者接口(interface)中的 Java 泛型类型参数

C++ 继承不继承

java - EJB 的数据源问题

java - 如何检查 Android 设备中的 voLTE 功能?

java - 找不到方法格式的符号(DateTimeFormatter)?

compilation - VHDL编译错误

debugging - SASS不一致的缩进错误