Create a Java program that asks the user for lines until you press Enter.
The lines should be updated at the end of the text file if it exists, but a new file should be created.
dapibus pretium radar
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class UpdateFile { public static void main(String[] args) { // Name of the file where the lines will be saved or updated String fileName = "updated_file.txt"; try { // Create a FileWriter object with the option to append to the existing file FileWriter file = new FileWriter(fileName, true); // Create a BufferedWriter object to write lines of text efficiently BufferedWriter writer = new BufferedWriter(file); // Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); System.out.println("Enter lines to write or update in the file (Press Enter to finish):"); // Read lines from the user until Enter is pressed String line = scanner.nextLine(); while (!line.isEmpty()) { // Write the line to the file writer.write(line); writer.newLine(); // Add a newline after each line // Read the next line line = scanner.nextLine(); } // Close the BufferedWriter and FileWriter writer.close(); file.close(); System.out.println("The lines have been written or updated in the file successfully."); } catch (IOException e) { // Handle possible input/output exceptions e.printStackTrace(); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a program in Java to request lines from the user and write them in a text file.
Create a Java program that reads a text file and displays all its lines on the screen.
Write a program in Java to read a text file and make a copy in another file by changing the lowercase letters to uppercase.
Create a program in Java to count the number of words stored in a text file.
Create a Java program that makes a copy of a text file but encrypted.
Create a Java program that reads the contents of a text file and stores it in an array of strings.
Create a Java program that makes a copy of a text file but reverse all its content.
Create a Java program that behaves like the Unix more command, you must display the contents of a text file and ask the user to press Enter every time the screen is full.
Practice with exercises in Java to manage text files. Learn how to create, update, and search text files in different ways.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.