10 Replies Latest reply: May 20, 2007 4:39 PM by 807606 RSS

    moving files into new folders (help needed)

    807606
      Hi it would be grate if some one could help me....

      in/with my program i need to move a file (mp3) from one folder into a new folder???
        • 1. Re: moving files into new folders (help needed)
          807606
          can any 1 help me???
          please....
          • 2. Re: moving files into new folders (help needed)
            807606
            http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
            you have to copy the file and then delete it
            • 3. Re: moving files into new folders (help needed)
              800282
              http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
              you have to copy the file and then delete it
              Or (try to) use the public boolean renameTo(File dest) method.
              • 4. Re: moving files into new folders (help needed)
                807606
                i like the idea of the rename to....to copy the file and delete would take long wouldn't it? (i duno)
                because the files are mp3

                (I've never worked with files be for I've only done the AP Computer Science A sub set) so yeah....

                well this is what i tried but it gives me

                rename to(java.io.File) in java.io.File cannot be applied to (java.lang.String)

                public boolean renameTo(File dest)
                {
                for(int index = 0; index < DirectoryList.files.length; index++)
                {
                File song = new File(DirectoryList.directoryName + File.separator + DirectoryList.files[index]);
                song.renameTo(DirectoryList.directoryName + File.separator + artist);
                }
                }
                • 5. Re: moving files into new folders (help needed)
                  800282
                  ...
                  renameTo(java.io.File) in java.io.File cannot be applied to (java.lang.String)
                  The compiler is telling you that you cannot put a String as a parameter. You need to provide an instance of a File.
                  It should look like this:
                  aFile.renameTo(new File("/path/to/your/removed/file"));
                  • 6. Re: moving files into new folders (help needed)
                    807606
                    like this???

                    i cant seem to get it to work ....
                    [the last line]
                    if (tag.equals("TAG")) 
                                        { 
                                            System.out.println("\nTitle: " + id3.substring(3, 32)); 
                                            System.out.println("Artist: " + id3.substring(33, 62)); 
                                            System.out.println("Album: " + id3.substring(63, 91)); 
                                            System.out.println("Year: " + id3.substring(93, 97));
                                            artist = id3.substring(33, 62);
                                            createFiles(); 
                                            song.renameTo(new File( DirectoryList.directoryName + File.separator + artist));
                                        } 
                    • 7. Re: moving files into new folders (help needed)
                      800282
                      like this???

                      i cant seem to get it to work ....
                      [the last line]
                      Define "cant seem to get it to work".

                      Try to debug your code: first ensure that the file really exists like this:
                      File newLocation = new File( DirectoryList.directoryName + File.separator + artist);
                      System.out.println("'song' exists? "+song.exists());
                      System.out.println("'newLocation' already exists? "+newLocation.exists());
                      System.out.println("Renaming succeeded? "+song.renameTo(newLocation));
                      Note that renameTo(...) might fail in some circumstances: look into the File API docs for details.
                      • 8. Re: moving files into new folders (help needed)
                        807606
                        well what i mean is the file is not being moved into a new foled

                        here is the out put for the debuging
                        'song' exists? true
                        'newLocation' already exists? true
                        Renaming succeeded? false
                        • 9. Re: moving files into new folders (help needed)
                          800282
                          well what i mean is the file is not being moved into
                          a new foled

                          here is the out put for the debuging

                          'song' exists? true
                          'newLocation' already exists? true
                          Renaming succeeded? false
                          It looks like you're overwriting a file since 'newLocation' already exists (I'm not sure if this is possible). Did you try renaming the file to a location where it doesn't overwrite an existing file?
                          • 10. Re: moving files into new folders (help needed)
                            807606
                            ...i was and i also tried with out it...here is my code (for both classes!)
                            it does not have the returnTo()...i took it out...this is working...but the mp3 files need to be moved into the files that have been created or already exist...

                            import java.io.File;
                            import cs1.Keyboard;
                             
                            public class DirectoryList  {
                             
                             /* This program lists the files in a directory specified by
                                the user.  The user is asked to type in a directory name.
                                If the name entered by the user is not a directory, a
                                message is printed and the program ends.
                             */
                             
                            static String directoryName;  // Directory name entered by the user.
                            static File directory;        // File object referring to the directory.
                            static String[] files;        // Array of file names in the directory.
                                String[] arguments;
                                
                             public static void main(String[] args) {
                             
                                
                                
                                System.out.print("Enter a directory name: ");
                                directoryName = Keyboard.readString().trim();
                                directory = new File(directoryName);
                             
                                if (directory.isDirectory() == false) {
                                    if (directory.exists() == false)
                                       System.out.print("There is no such directory!");
                                    else
                                       System.out.print("That file is not a directory.");
                                }
                                else {
                                    files = directory.list();
                                    System.out.println("Files in directory \"" + directory + "\":");
                                    for (int i = 0; i < files.length; i++)
                                    {
                                        System.out.println("   " + files);
                            }

                            }
                            } // end main()
                            }




                            import java.io.*; 
                             
                            public class ReadID3 
                            { 
                                static String artist;
                                static int index = 0;
                                
                                public static void main(String[] arguments) 
                                {    
                                    DirectoryList.main(arguments);
                                    
                                    if (DirectoryList.directory.exists() == true)
                                    {
                                    for(int index = 0; index < DirectoryList.files.length; index++)
                                    {
                                        
                                        try 
                                        { 
                                            File song = new File(DirectoryList.directoryName + File.separator + DirectoryList.files[index]); 
                                            FileInputStream file = new FileInputStream(song); 
                                            int size = (int)song.length(); 
                                            file.skip(size - 128); 
                                            byte[] last128 = new byte[128]; 
                                            file.read(last128); 
                                            String id3 = new String(last128); 
                                            String tag = id3.substring(0, 3);
                                        
                                                if (tag.equals("TAG")) 
                                                { 
                                                    System.out.println("\nTitle: " + id3.substring(3, 32)); 
                                                    System.out.println("Artist: " + id3.substring(33, 62)); 
                                                    System.out.println("Album: " + id3.substring(63, 91)); 
                                                    System.out.println("Year: " + id3.substring(93, 97));
                                                    artist = id3.substring(33, 62);
                                                    createFiles();
                                                    
                                                        File newLocation = new File( DirectoryList.directoryName + File.separator + artist);
                                                        System.out.println("'song' exists? "+song.exists());
                                                        System.out.println("'newLocation' already exists? "+newLocation.exists());
                                                        System.out.println("Renaming succeeded? "+song.renameTo(newLocation));
                                                } 
                                        
                                                else 
                                                    System.out.println(arguments[0] + " does not contain" 
                                                    + " ID3 info.");  
                                                    file.close(); 
                                        } 
                                        
                                        catch (Exception e) 
                                        { 
                                            System.out.println("Error � " + e.toString()); 
                                        }
                                    } 
                                }
                                }
                            
                                public static void createFiles()
                                {
                                        File f = new File(DirectoryList.directoryName + File.separator + artist);
                                        f.mkdir();
                                }
                            }