I am making a program which hopefully will organise my desktop because I am god awful at doing that.
I've just learnt how to make a program that will move a particular file from one location to another as well as list all the files within a particular directory so I can determine which files I want to move.
The problem is, is that the file being moved has to be known already in the sense you have to give the file name and extension. What I want to do is make it so the program scans the directory, presents this to the user, asks what they want to sort, the program then looks for all the files which are relevant to what the user wants to sort via a keyword and is then moved to the appropriate folder.
I've tried making a variable which contains the name of the file and adding that to the line of code which didn't work.
String fileMove = null;
File folder = new File("/Users/james/Desktop/Desktop2");
File[] listOfFiles = folder.listFiles();
System.out.println("The files currently in this directory are:");
for (int i = 0; i <listOfFiles.length; i++) {
System.out.println(listOfFiles[i].getName());
}
System.out.println("");
System.out.println("What files do you want to sort?");
String userResponse = userInput.nextLine();
if (userResponse.equals("English")) {
for (int i = 0; i <listOfFiles.length; i++) {
if(listOfFiles[i].getName().contains("English")) {
fileMove = listOfFiles[i].getName();
}
}
File sourceFile = new File("/Users/james/Desktop/Desktop2/ " + fileMove);
File destinationFile = new File("/Users/james/Desktop/Desktop2/English " + fileMove);
System.out.println("Operation complete");
This program was expected to: List the files in a directory, asks the user what files are to be sorted, sort the files.
The program actually: Lists the files in a directory, asks the user what files are to be sorted, n/a