Needed help Writing to a File using try and catch block. Use FileWriter and IOException classes.
Added by Clara G.
Step 1
First, we need to create a FileWriter object to write to a file. We can do this by passing the file name and the boolean value 'true' to the constructor. The boolean value 'true' indicates that we want to append to the file if it already exists. Show more…
Show all steps
Your feedback will help us improve your experience
Patricia Holmes and 98 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
To avoid an IOException exception with files,you can either use a try...catch block or make sure there is a file before attempting to read characters from it.This can be done by: a. calling the File.Exists( ) method b. using a loop to cycle through the file structure c. throwing an exception d. including statements in a finally block e. placing a test loop in the Main( ) method
/** * Prompt the user to enter a filename, then use the provided * Scanner object to read the entire line of user input as * a String value. Use this value to create a new File object, * then use the exists() and isFile() methods to check that * the user-specified file is valid. If the file does not * exist or is not a file, display an error message and prompt * the user again. If the file is valid, return the File object * to the caller. * @param kbd Scanner object bound to System.in * @return Valid File object that represents the filename entered by the user. */ public static File getFile(Scanner kbd) {...}
Supreeta N.
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. Save the file as EvenOdd.java.
Shelayah R.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD