Java程序错误说预期的类,接口或枚举

时间:2015-02-26 09:39:37

标签: java

import java.io.*;

            public class Test13
    {
            public static void main(String[] args) throws IOException 

    {

    FileInputStream fis1 = new FileInputStream("D:/abc.txt");

    FileInputStream fis2 = new FileInputStream("D:/xyz.txt");

    SequenceInputStream sis = new SequenceInputStream(fis1,fis2);

    int i;

    while((i = sis.read())!=-1)

    {

    System.out.println((char)i);

    }

    }
    }


    catch(Exception ex)

    {

     ex.printStackTrace();

    }

    }

    }

3 个答案:

答案 0 :(得分:1)

我觉得你尝试过类似的东西。我在Exception-Handling

中插入了一些explenations
import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class Test13 {

    //because all exceptions are already catched main will never throw one 
    public static void main(String[] args) throws IOException {
        try {
            //if an exception raises anywhere from here ...

            FileInputStream fis1 = new FileInputStream("D:/abc.txt");
            FileInputStream fis2 = new FileInputStream("D:/xyz.txt");
            SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

            int i;
            while ((i = sis.read()) != -1) {
                System.out.println((char) i);
            }

            //... to here ...

        } catch (Exception ex) {
            //this catch block code will be executed
            ex.printStackTrace();           
        }
    }

}

答案 1 :(得分:0)

Below is the updated code and its working fine.

    import java.io.*;

    public class Test13

    {
        public static void main(String[] args) throws IOException {
            try {
                FileInputStream fis1 = new FileInputStream("D:/abc.txt");

                FileInputStream fis2 = new FileInputStream("D:/xyz.txt");

                SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

                int i;

                while ((i = sis.read()) != -1)

                {

                    System.out.println((char) i);

                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

答案 2 :(得分:0)

你应该

   try{
         //function which throws exceptions
      }
   catch( SpecificException e ) {
         // if a specific exception was thrown, handle it here
    }
   catch(Exception ex) {        
        // if a more general exception was thrown, handle it here
      }
    finally{
       }