Results 1 to 4 of 4
- 11-11-2012, 02:05 AM #1
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:
It will make all the folders but it won't move the files.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()); } }
Any Ideas?
- 11-13-2012, 04:54 PM #2
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.
- 11-14-2012, 05:27 PM #3
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
- 11-14-2012, 10:54 PM #4
Re: Make a folder and move a file to it
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.I have tried renameTo(), a native OS command and the Apache Ant but none would work for me
Similar Threads
-
I need to make chess pieces visible and select two squares to make any pieces to move
By Sufiyana in forum New To JavaReplies: 2Last Post: 10-31-2012, 04:31 PM -
About creating a dll and bat, executing and maybe move from a folder to another
By LuisEduardo in forum Advanced JavaReplies: 1Last Post: 08-22-2011, 05:23 PM -
java mail, using POP3, move a message to another folder?
By new_2_java in forum New To JavaReplies: 1Last Post: 05-18-2011, 02:08 AM -
To make image move
By mneskovic in forum New To JavaReplies: 4Last Post: 05-08-2010, 08:23 PM -
How do I make My ball to move randomly?
By whdbstjr90 in forum New To JavaReplies: 4Last Post: 12-31-2007, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks