-4
File file = new File("D:/projects/tFile.txt") ;
        file.createNewFile();  //Unhandled exception type IOException
        FileOutputStream fout = new FileOutputStream(file); //Unhandled exception type FileNotFoundException
        String s = "Cricket";
        byte []b = s.getBytes();
        fout.write(b);//Unhandled exception type IOException
        fout.close();// Unhandled exception type IOException

This is showing FileNotFound Exception and IoException.

Sam
  • 7
  • 3
  • 1
    When you have a question about errors, please make sure to include in your question whether the error is compile-time or runtime, what line the error occurs on, and the *complete* error text. – azurefrog Apr 12 '16 at 19:00
  • Did you bother to Google the errors to make an effort to understand what they are, why they are happening, and what some potential fixes might be? – tnw Apr 12 '16 at 19:00
  • Did you even read the error messages? – SLaks Apr 12 '16 at 19:02

2 Answers2

-1

It is asking you to handle the Exceptions that are being thrown from the methods that you have used.

Surround them in a try catch block.

public void method(){
        try {
            File file = new File("D:/projects/tFile.txt") ;
            file.createNewFile();
            FileOutputStream fout = new FileOutputStream(file);
            String s = "IPL is very entertaining tournament";
            byte []b = s.getBytes();
            fout.write(b);;
            fout.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
  • This doesn't really *fix* the issue. – tnw Apr 12 '16 at 19:08
  • Can you please elaborate? – Pavan Andhukuri Apr 12 '16 at 19:09
  • All this does is keep the exceptions from crashing the program. The exceptions are still thrown and the problem is not fixed. I therefore don't think that this is a valid answer. – tnw Apr 12 '16 at 19:10
  • https://docs.oracle.com/javase/6/docs/api/java/io/File.html#createNewFile() Create new file throws IOException as per the Javadoc API. Will the code get compiled without adding exception handling? – Pavan Andhukuri Apr 12 '16 at 19:12
  • Not sure what your point is. Your answer still doesn't address the actual problem. – tnw Apr 12 '16 at 19:13
  • I request you to try pasting the code in eclipse. I guess the errors mentioned in the question are compile time errors. Not runtime. Cause IOException and FileNotFoundException are the exact exceptions that are thrown with the above code. – Pavan Andhukuri Apr 12 '16 at 19:14
  • Thanks Pavan, issue is fixed. – Sam Apr 12 '16 at 19:16
  • Those are both runtime exceptions. An exception, by definition, happens at runtime. @SsumitChaudhary This does not fix your issue. You have some sort of permissions issue that is just being covered up by this code. – tnw Apr 12 '16 at 19:19
  • I have a doubt, is it necessary to encapsulate the code in try catch block.As exceptions are being thrown implicitly. – Sam Apr 12 '16 at 19:20
  • http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ Request you to go through that link and understand. I rest my case. – Pavan Andhukuri Apr 12 '16 at 19:20
  • 2
    But if it is permission issue how it can be solved? – Sam Apr 12 '16 at 19:21
  • @SsumitChaudhary You can also add throws clause to your method definition. Let the calling method handle the exception. – Pavan Andhukuri Apr 12 '16 at 19:21
  • Got it Pavan, thanks a lot. – Sam Apr 12 '16 at 19:31
-2

This is executed fine.Might be ur D:/projects is missing. or may be due to exception

public static void main(String[] args) throws IOException {
    File file = new File("D:/Test/tFile.txt") ;
    file.createNewFile();
    FileOutputStream fout = new FileOutputStream(file);
    String s = "IPL is very entertaining tournament";
    byte []b = s.getBytes();
    fout.write(b);;
    fout.close();
}
Haridwar Jha
  • 1
  • 1
  • 2