🎴 Flashcard Mode
Java Programming Fundamentals
Card1 / 13
Mastered0
Review0
QuestionClick to flip
- Assume that in the current directory test.txt file is already exist. What will happen when you execute the following code? import java.io.*; class Test { public static void main(String args[]) { try { File file = new File("test.txt"); file.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); } } }
AnswerClick to flip back
A
exception will be thrown
💡 Explanation:
File.createNewFile() creates the file ONLY if it does not already exist. If the file exists (like test.txt in this case), the method returns false and does nothing - it does NOT overwrite. This method only throws IOException if there's a directory issue or permission problem, not if the file exists. Option A is wrong because existing files are not overwritten.