Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: A few questions

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A few questions

    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
    Last edited by adenverd; May 25th, 2010 at 12:43 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: A few questions

    Hello there,

    1. Yes, since the backslash is a special character in Java and used as an escape character you need to escape it using the backslash it self so yes \\ is actually outputted as a single backslash in Java.

    2. This of course is a design question, it all depends on the design of your program but in this case I'd say it works really well as you can reuse the method by just passing in a string to another directory.

    3. As a part of question 2 really but you could start out with renaming checkFile1 and checkFile2 methods to actually make more sense. Another thing you might want to consider is things such as calling new File inside loops, in checkFile1 you know that the variable dir will always be the same, its a File object pointing to the directoryName passed into the method, so what you can do is create this file object at the top of the method, outside the loop, this will give you a small performance boost, especially when looping through a lot of files. When you first create your scanner you can do something like instead.

    sc = new Scanner(inputFileName).useDelimiter("\\");

    This will create the scanner object for you and set the delimiter so now you dont have to set the delimiter inside the checkFile methods.

    Another really important thing is that you should always close and streams you have opened, such as the BufferedReader in duplicateDeleter. So what you can do is at the bottom, right after the catch clause you add this.

    finally {
        if(br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
    }

    The above code will insure that no nasty memory leaks happen.

    I'm sure there might be a couple of more tiny things you can do to enhance your code but this should get you started. Hope that helps.

    // Json

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A few questions

    don't want to deal with deleting the wrong files until I have a few questions answered:
    To prevent this, put some conditional code in the program that will println() a message about what will be done vs actually doing it. And to be really sure while debugging the code, comment out the delete until your really sure its working the way you want.
        if(Testing) 
           S....println("file:" + fn + " will be deleted");
        else
          file.delete();

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: A few questions

    Either that or take a small sample copy of the directory and files you wish to do this on and run against that copy.

    // Json

Similar Threads

  1. [SOLVED] Some serious questions,
    By Time in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2010, 02:52 AM
  2. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  3. A Few Quick, Easy Questions
    By TheAsianMenace in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:47 PM
  4. Java Questions! :)
    By xs4rdx in forum Java Theory & Questions
    Replies: 0
    Last Post: February 21st, 2010, 08:40 AM
  5. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM