Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Surf directory - Practice Exercises Java


Lesson 12:

Additional libraries


Exercise 12.9:

Surf directory


Objetive:

Create a program to display the files in the current directory, and to allow the user to move up and down in that list. If the user presses Enter on a directory name, he will enter that directory; if he presses Enter on a file, that file will be launched.


Source Code:


import java.util.*;
public class Main
{
	private static int position = 0;

	private static ArrayList items;

	private static String directory = ".";

	static void main(String[] args)
	{
		while (true)
		{
			Console.Clear();

			items = GetItems(directory);

			ShowItems();
			ShowIndications();
			ReadKeys();
			Thread.sleep(200);
		}
	}

	private static void OpenFile(Item item)
	{
		if (item.getIsFile())
		{
			Process.Start(item.getName());
		}
	}

	private static void ReadKeys()
	{
		ConsoleKeyInfo key = Console.ReadKey();
		switch (key.Key)
		{
			case UpArrow:
				if (position > 0)
				{
					position--;
				}
				break;
			case DownArrow:
				if (position < items.size() - 1)
				{
					position++;
				}
				break;
			case Enter:
				Item item = items.get(position);

				if (item.getIsFile())
				{
					OpenFile(item);
				}
				else
				{
					directory = item.getPath();
				}
				break;
		}
	}

	private static void ShowSelected(int i)
	{
		if (i == position)
		{
			Console.SetCursorPosition(0, position);
			Console.BackgroundColor = ConsoleColor.DarkCyan;
		}
		else
		{
			Console.BackgroundColor = ConsoleColor.Black;
		}
	}

	private static ArrayList GetItems(String direc)
	{
		try
		{
			ArrayList items = new List();

			String[] directories = (new java.io.File(direc)).list(java.io.File::isDirectory);
			for (String directory : directories)
			{
				items.add(new Item(directory, false));
			}

			String[] files = (new java.io.File(direc)).list(java.io.File::isFile);
			for (String file : files)
			{
				items.add(new Item(file, true));
			}

			return items;
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error reading items.");
			return null;
		}
	}

	private static void ShowItems()
	{
		int i = 0;
		for (Item item : items)
		{
			ShowSelected(i);
			System.out.println(item.getPath());
			i++;
		}
		Console.BackgroundColor = ConsoleColor.Black;
	}

	private static void ShowIndications()
	{
		Console.SetCursorPosition(0, 23);
		System.out.println("Press arrow up for move up  | Press arrow down for move down");
	}
}

public class Item
{
	private String Path;
	public final String getPath()
	{
		return Path;
	}
	public final void setPath(String value)
	{
		Path = value;
	}

	private boolean IsFile;
	public final boolean getIsFile()
	{
		return IsFile;
	}
	public final void setIsFile(boolean value)
	{
		IsFile = value;
	}

	public final String getName()
	{
		return getPath().substring(2);
	}

	public Item(String path, boolean isFile)
	{
		this.setPath(path);
		this.setIsFile(isFile);
	}
}
Exercisey 12.9

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.