More Learn programming Java

Lesson:

File Management


Exercise:

More 9


Objetive:

Create a program which behaves like the Unix command "more": it must display the contents of a text file, and ask the user to press Enter each time the screen is full.

As a simple approach, you can display the lines truncated to 79 characters, and stop after each 24 lines


Code:

package More;
import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
	}

	public final void ShowData(String urlFile)
	{
		java.io.InputStreamReader fichero = new java.io.InputStreamReader(urlFile);
		String line;
		int count = 0;
		do
		{
			line = fichero.ReadLine();
			if (line != null)
			{
				if (count % 24 == 0)
				{
					new Scanner(System.in).nextLine();
				}
				if (line.length() > 79)
				{
					line = line.substring(0, 79);
				}
				System.out.println(line);
			}
			count++;
		} while (line != null);
		fichero.close();
	}
}