unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

前端 未结 5 1563
后悔当初
后悔当初 2020-12-11 22:58

I am creating a class -- just a class, no main() and I am receiving the error of \"unreported exception java.io.FileNotFoundException; must be caught or declared to be throw

5条回答
  •  天涯浪人
    2020-12-11 23:31

    You need to put the file processing statements inside a method:

    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    
    public class UseLoggingOutputStream {
        public void myMethod() {
            String file = "c:\\system.txt";
            try {
                FileOutputStream outStr = new FileOutputStream(file, true);
            } catch(FileNotFoundException fnfe) { 
                System.out.println(fnfe.getMessage());
            } 
        }
    }
    

提交回复
热议问题