I've only taken a semester of java, so I only know the basics here. I wrote this without guidance from my teacher, so I have a few questions about whether or not some of the stuff will work. Basically, what this program is supposed to do is take the text output of another program that lists duplicate files in a directory. My program will then take those files and check the file type (in this case, .mp3 and .mp4, because I'm merging music libraries and don't want copies) and, based on user input, either move one of each set of duplicates to a new folder as specified by the user or delete one of the duplicates. Here's an example of the output file I'm working from, and my code:
2 files size 49,793 checksum 66d368b669fd37e2f8e6f2156c004e82:
C:\Users\Sawyer\Music\iTunes\iTunes Music\Third Eye Blind\Out Of The Vein\AlbumArt_{4866D027-C7C0-4B50-9597-512D5597A3F9}_Large.jpg
C:\Users\Sawyer\Music\iTunes\iTunes Music\Third Eye Blind\Out Of The Vein\Folder.jpg
2 files size 3,163,090 checksum 28d502ad590d26bc245ac0c06f9414ad:
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Killers\Hot Fuss [US]\04 Somebody Told Me 1.mp3
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Killers\Hot Fuss [US]\04 Somebody Told Me.mp3
2 files size 3,594,240 checksum 5790c6231855e94ee0c1236de81dc0cf:
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Who\Who's next\08 Behind the Blue Eyes 1.mp3
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Who\Who's next\08 Behind the Blue Eyes.mp3
2 files size 5,103,252 checksum df9daa7ebf34b66329f5d910b9aec54d:
C:\Users\Sawyer\Music\iTunes\iTunes Music\The White Stripes\Elephant\09 09 09 09 The Hardest Button To Bu.mp3
C:\Users\Sawyer\Music\iTunes\iTunes Music\The White Stripes\Elephant\09 09 09 The Hardest Button To Butto.mp3
2 files size 5,472,576 checksum 5ff8cec013b757b24d6220184b0ce94f:
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Killers\Hot Fuss\01 Mr Brightside 1.mp3
C:\Users\Sawyer\Music\iTunes\iTunes Music\The Killers\Hot Fuss\01 Mr Brightside.mp3
2 files size 8,018,057 checksum ec6a56521f4bf33fd32aebf986cdd025:
C:\Users\Sawyer\Music\iTunes\iTunes Music\Weezer\Make Believe\01 Beverly Hills 1.mp3
C:\Users\Sawyer\Music\iTunes\iTunes Music\Weezer\Make Believe\01 Beverly Hills.mp3
Found 4,819 files and 905 folders with 480 possible duplicates.
Deleting duplicates would save 29,336,709 bytes of disk space.import java.io.*; import java.util.*; public class duplicateDeleter { private BufferedReader br; private Scanner sc; public String inputFileName; public duplicateDeleter (String infile) { String s, t; inputFileName = infile; sc = new Scanner (inputFileName); System.out.println("Would you like to:"); System.out.println("1. Send one of each set of duplicates to a separate folder to be checked"); System.out.println("2. Delete one of the duplicates automatically"); br = new BufferedReader (new InputStreamReader(System.in)); try {s = br.readLine(); switch (Integer.parseInt(s)){ case 1: {System.out.println("What is the full directory path of the folder you want them moved to (doesn't have to exist)?"); t = br.readLine(); checkFile1(t);} case 2: checkFile2(); default: System.out.println ("Invalid input, please restart and enter either 1 or 2."); } } catch (IOException e){ System.out.println ("I/O Exception in your input, please enter only 1, 2, and a valid directory path"); } } public void checkFile1(String directoryName){ String s; sc.useDelimiter("\\"); /*sets the delimiter to a backslash (refer to output example for the reason why)*/ while ((sc.hasNextLine()) == true){ if (sc.hasNext("*.mp3") == true || sc.hasNext("*.mp4")== true ){ /*if the next token is a .mp3 or .mp4 file*/ s=sc.next(); File file = new File(s); /*Takes the current token and uses it as the file name for a new file */ File dir = new File(directoryName); file.renameTo(new File(dir, file.getName())); /*Moves the file to the new directory*/ } else { sc.next(); } sc.nextLine(); } System.out.println("Duplicate files have been moved to " + directoryName); } public void checkFile2(){ String s; sc.useDelimiter("\\"); while ((sc.hasNextLine()) == true){ if (sc.hasNext("*.mp3") == true || sc.hasNext("*.mp4")== true ){ s=sc.next(); File f = new File(s); // Make sure the file or directory exists and isn't write protected if (!f.exists()) throw new IllegalArgumentException( "Delete: no such file or directory: " + s); if (!f.canWrite()) throw new IllegalArgumentException("Delete: write protected: " + s); // Attempt to delete it boolean success = f.delete(); if (!success) throw new IllegalArgumentException("Delete: deletion failed"); } else { sc.next(); } sc.nextLine(); } System.out.println("Duplicate files have been deleted."); } }
Haven't tested the program as I don't want to deal with deleting the wrong files until I have a few questions answered:
1. The line sc.useDelimiter ("\\"); will use a single backslash as the delimiter, right?
2. Did I do the whole sending a string thing right? Namely when I ask for the directory name and then send it to the checkFile1 method.
3. Do you see any other errors, or ways I could improve upon this program?
Thanks in advance from a newbie
-A'den