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.
As a simple method, you can display truncated lines at 79 characters and stop after every 24 lines.
Lorem ipsum dolor sit amet consectetur adipiscing elit nulla commodo facilisis. Est litora vitae imperdiet senectus sed purus lacus fermentum libero. Odio faucibus nibh massa per euismod etiam netus nunc. Mus ad aliquam non fermentum eu libero eros nullam varius curabitur, ligula et sem sapien eget fri convallis nostra suscipit, metus egestas curae penatibus potenti fames urna vulputate himenaeos. Venenatis dapibus congue quis aptent tincidunt vivamus hendrerit litora purus, sollicitudin vestibulum malesuada fusce urna neque luct Curabitur netus dictum in mollis bibendum auctor ante, molestie suspendisse hab nostra tempor eu facilisi, nulla quam potenti integer aenean nisi. Litora vivam euismod justo sociis malesuada aliquet leo pretium nullam ullamcorper vitae nos dis facilisis vehicula, taciti molestie semper aliquam aptent fermentum maecena felis commodo blandit neque dui dapibus donec. Id erat curabitur sem dapibus er feugiat per phasellus enim class mauris auctor, tortor magnis nisi vivamus veli Curabitur netus dictum in mollis bibendum auctor ante, molestie suspendisse hab nostra tempor eu facilisi, nulla quam potenti integer aenean nisi. Litora vivam euismod justo sociis malesuada aliquet leo pretium nullam ullamcorper vitae nos dis facilisis vehicula, taciti molestie semper aliquam aptent fermentum maecena felis commodo blandit neque dui dapibus donec. Id erat curabitur sem dapibus er feugiat per phasellus enim class mauris auctor, tortor magnis nisi vivamus veli Lorem ipsum dolor sit amet consectetur adipiscing elit nulla commodo facilisis. Est litora vitae imperdiet senectus sed purus lacus fermentum libero. Odio faucibus nibh massa per euismod etiam netus nunc. Mus ad aliquam non Press Enter to continue...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MoreCommand { public static void main(String[] args) { // Path of the text file String filePath = "text_file.txt"; try { // Create a FileReader object to read the text file FileReader file = new FileReader(filePath); BufferedReader reader = new BufferedReader(file); // Maximum characters per line int maxCharactersPerLine = 79; // Maximum number of lines to display before waiting for user input int maxLinesPerScreen = 24; // Counters int lineCounter = 0; int characterCounter = 0; // Read the first line of the file String line = reader.readLine(); // Iterate over all lines of the file while (line != null) { // Truncate the line to maxCharactersPerLine if necessary if (line.length() > maxCharactersPerLine) { line = line.substring(0, maxCharactersPerLine); } // Display the line in the console System.out.println(line); // Increment counters lineCounter++; characterCounter += line.length(); // If the maximum number of lines per screen is reached, wait for user input if (lineCounter == maxLinesPerScreen) { System.out.println("Press Enter to continue..."); System.in.read(); // Wait for user input lineCounter = 0; } // Read the next line line = reader.readLine(); } // Close the BufferedReader and FileReader reader.close(); file.close(); } 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.
Create a Java program that asks the user for lines until you press Enter.
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.
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.