【问题标题】:Getting 'illegal start of type' when compiling编译时获取“非法类型开始”
【发布时间】:2013-05-17 09:16:07
【问题描述】:
 public class WriteByteArrayToFile {
    public static void main(String[] args)
    {
        String strFilePath = "C://Program Files/Java/jdk1.7.0_23/bin//s.excel";
        try
        {
            FileOutputStream fos = new FileOutputStream(strFilePath);
            int numberBytes = fileinputstream.available();
            byte bytearray[] = new byte[numberBytes];
            fileinputstream.read(bytearray);
            for(int i= 0;i < numberBytes;i++)
            {
                System.out.println(bytearray[i]);
            }
            fileinputstream.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
            MyReader mr = new MyReader();
            mr.ReadFile("side1-60.java");
        }

        String strContent = "side1-60.java ";
        fos.write(strContent.getBytes());
        fos.close();
    }
    catch(FileNotFoundException e)
    {
        System.out.println("FileNotFoundException : " + ex);
    }
    catch(IOException ioe) {    
        System.out.println("IOException : " + ioe);
    }
}

这是我的代码。我在编译时收到illegal start of type 的错误,尤其是在catch(FileNotFoundException e)。任何人都可以帮助我克服这个问题吗?

【问题讨论】:

  • 如果你合理地缩进你的代码,你会看到你的最后两个 catch 块附加到你的 main 方法,而不是你的 try 块。
  • @system32 不会导致此错误...

标签: java compiler-errors


【解决方案1】:

只有一个 try 块的代码。

 catch(Exception e)
                    {
                    System.out.println(e);
        MyReader mr = new MyReader();
        mr.ReadFile("side1-60.java");           
        }

//此代码导致问题,catch 块没有继续代码

    String strContent = "side1-60.java ";

     fos.write(strContent.getBytes());

     fos.close();

catch(FileNotFoundException f){
}

【讨论】:

    【解决方案2】:

    我编辑了您的代码并修复了缩进 - 您每次编写代码时都应该这样做。如您现在所见,您正在关闭 try-catch 块,然后执行更多代码。

    然后您将关闭该方法 - 之后您将拥有您的 catch(FileNotFoundException e)。这个 catch 块在方法完成后出现 - 因此需要一个成员变量或另一个方法 - 因此你会收到这个错误。

    由于我不知道你的逻辑,我无法告诉你它需要是什么 - 但现在的方式肯定是错误的。

    【讨论】:

      【解决方案3】:

      您的代码无法运行,您忘记声明“fileinputstream”并且您的 try-catch 无效。您尝试在两个 catch-Block 之间编写代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-20
        • 1970-01-01
        • 2014-12-17
        • 1970-01-01
        • 2021-02-18
        相关资源
        最近更新 更多