Jump to content

Implementation of a File Lister and File Collector

The basis of the assignment I have is that I am to write two programs. One that lists all the files of a folder that match a certain extension (folder, output file, and extension passed as arguments) and one that organises the files of a folder into a given number of collections without exceeding a given size per collection (number and size of collections given as arguments on top of the same arguments for the first program). The programs are to be executed directly from the console so like:

java ca.cmpt213.as1.FileLister <source folder> <target file> [<extensions>] 

 Extensions is in square brackets as it's an optional argument. The target file is the txt file where the program will write the output to.

 

What I want to know is how I should set up these programs as a project in IntelliJ. I was told to simply create a Java project using the Command Line App template. But what's confusing me is that the programs don't seem to be called from a Main Class. Rather, they are called separately from the terminal so am I supposed to write anything in the auto-generated Main class or just delete the Main class and have a main method in the java class of each program. I tried the latter and IntelliJ is complaining about not being able to find the Main class so I'm sure that's not what I should be doing.

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used IntelliJ so I can't comment about the Command Line App template, but you need one main method for each program. The main method accepts the command line arguments as a String array parameter.

For example:

public class FileLister
{
	public static void main(String[] commandLineArguments)
	{
		String source = commandLineArguments[0];
		String target = commandLineArguments[1];
		int startingIndexOfExtensions = 2;
		String[] extensions = new String[commandLineArguments.length - startingIndexOfExtensions];
		for (int i = startingIndexOfExtensions; i < commandLineArguments.length; i++)
		{
			extensions[i - startingIndexOfExtensions] = commandLineArguments[startingIndexOfExtensions];
		}
		
		listFilesWithExtension(source, target, extensions);
	}
	
	static void listFilesWithExtension(String folder, String target, String[] extensions)
	{
		System.out.println("Folder: " + folder);
		System.out.println("Target: " + target);
		for (String extension : extensions)
		{
			System.out.println("Extension: " + extension);
		}
	}
}

You can compile this using:

javac FileLister.java

And run the program using:

java FileLister ThisIsMyFolder ThisIsMyTarget ThisIsMyFirstExtension ThisIsMySecondExtension

Which will produce the following output:

Folder: ThisIsMyFolder
Target: ThisIsMyTarget
Extension: ThisIsMyFirstExtension
Extension: ThisIsMyFirstExtension

 

Take a look here for more information about command line arguments: https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

Link to comment
Share on other sites

Link to post
Share on other sites

On ‎2017‎-‎01‎-‎19 at 3:35 PM, PlutoNZL said:

-snip-

Thanks for the tips! I got it working properly with my arguments. One other question though. I'm using PrintWriter to write text onto a text document created within the program. I can use print() and println() to write to the text document with no problems. But when I try to do formatted printing like:

writer.println("Source Path: %s", sourceFolder);

where sourceFolder is a string denoting a folder path, it gives me a "Cannot resolve method 'println(java.lang.string, java.lang.string)' " error. I would like to use formatted printing instead of doing separate print statements to print the folder paths. In fact, I have to when printing file sizes as I have to write the file size in bytes with separating commas.

 

It won't even allow me to use the new line specifier.

writer.println("********************************** %n");

 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, BlueChinchillaEatingDorito said:

Thanks for the tips! I got it working properly with my arguments. One other question though. I'm using PrintWriter to write text onto a text document created within the program. I can use print() and println() to write to the text document with no problems. But when I try to do formatted printing like:


writer.println("Source Path: %s", sourceFolder);

where sourceFolder is a string denoting a folder path, it gives me a "Cannot resolve method 'println(java.lang.string, java.lang.string)' " error. I would like to use formatted printing instead of doing separate print statements to print the folder paths. In fact, I have to when printing file sizes as I have to write the file size in bytes with separating commas.

 

It won't even allow me to use the new line specifier.


writer.println("********************************** %n");

 

You need to use the printf method: https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#printf(java.lang.String, java.lang.Object...)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×