Results 1 to 4 of 4
  1. #1
    rosaage's Avatar
    rosaage is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Question Make a folder and move a file to it

    A couple of months ago my friend asked for a program to make lots of folders
    so i made one where you type name then how many (if name is T and number is 3 it will make T 1, T 2, and T 3
    but he asked for more: move some files to these folders the files i want to move is episodes so they are labeled with episode numbers so i tried this:

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		  try{
    		  int iint = 0;
    		  int aInt = Integer.parseInt(textField_1.getText()) + 1;
    		  String strDirectoy = textField.getText() + (iint + 1);
    		  while(iint < aInt) {
    			  iint = iint + 1;
    			  new File(strDirectoy).mkdir();
    			  strDirectoy = textField.getText() + (iint);}
    		      Path p = Paths.get(textField_2.getText() + iint + textField_3.getText());
    		      Path pp = Paths.get(strDirectoy +"/"+ p.toFile().getName());
    			  Files.move(p, pp);
    		      
    		}catch (Exception ee){
    		 System.err.println("Error: " + ee.getMessage());
    		}	  
    	}
    It will make all the folders but it won't move the files.
    Any Ideas?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Make a folder and move a file to it

    What is Files.move()? Is that a method you wrote or are you asking how to write one?

    There are several ways to move files. I'm not sure Java's File class supports it on your file system, but you can try using the renameTo() method. This would work in a similar manner to mv in *nix, just provide a destination path. Another technique which might work if the last fails is a copy move (inefficient, but might be fine for small files). In this case you simply write out a copy of each file to the new destination and delete the original.

    A more or less hacky way to do it might be to use your underlying OS to do it for you. Execute a native OS command from java (such as mv) and supply it with the source and destination paths.

    A final way to do it is to use an external library to do it for you. Apache Ant has pretty simple file tools (moving, copying, renaming etc...) and I find myself using this technique the most when I code in grails (since ant tasks are built in, so no special imports are even required!). The nice thing about this approach is that it's platform independent.

  3. #3
    rosaage's Avatar
    rosaage is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Make a folder and move a file to it

    I have tried renameTo(), a native OS command and the Apache Ant but none would work for me
    and about Files.move():
    java.nio.file.Files

    This class consists exclusively of static methods that operate on files, directories, or other types of files.

    In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

    Since:
    1.7

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Make a folder and move a file to it

    I have tried renameTo(), a native OS command and the Apache Ant but none would work for me
    I seriously doubt none of them work. Lets see some code and errors, my guess is you either didn't use them correctly, or you are attempting to work with files that your own user account on the computer does not have access to.

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2012, 05:31 PM
  2. Replies: 1
    Last Post: 08-22-2011, 06:23 PM
  3. Replies: 1
    Last Post: 05-18-2011, 03:08 AM
  4. To make image move
    By mneskovic in forum New To Java
    Replies: 4
    Last Post: 05-08-2010, 09:23 PM
  5. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 06:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •