• Post Reply Bookmark Topic Watch Topic
  • New Topic

Move a file to a specific folder  RSS feed

 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to move a file to a specified folder but I can't.
This is the code:



I would like to move the song file in the folder dir. To do this I have tried several methods:

- Files.move -> The following errors are generated:


- song.renameTo(dir) -> It does nothing.

- FileUtils.moveFile(song, dir) -> It does nothing.

Obviously the file isn't open anywhere else, I don't have a music player open.
How can I fix? I don't know what to do, I searched the internet but haven't yet been able to solve..

Thanks a lot.
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen applications hang onto file locks after the file has been closed so I suggest you reboot your system and then run your code before opening the file in any other application.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I've seen applications hang onto file locks after the file has been closed so I suggest you reboot your system and then run your code before opening the file in any other application.

Hi, I tried but nothing.. There is always the same error..
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try manually moving the file.
Try manually moving the file whilst your program is up an running but has not yet tried to move the file.
Try manually moving the file whilst your program is up an running and has just failed to move the file.
Try running a program that does nothing but moves the file.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thanks for the advice.

Tony Docherty wrote:Try manually moving the file.

It works.

Tony Docherty wrote:Try manually moving the file whilst your program is up an running but has not yet tried to move the file.

It works.

Tony Docherty wrote:Try manually moving the file whilst your program is up an running and has just failed to move the file.

It doesn't works. The error message is:


Tony Docherty wrote:Try running a program that does nothing but moves the file.

I did a simple program that moves an .mp3 file from one directory to another and it works. This program has no interface so I think the problem is the GUI... But maybe I'm wrong.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere in your program, some code is locking the file. This could be by trying to read it and not close any input streams to it.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Somewhere in your program, some code is locking the file. This could be by trying to read it and not close any input streams to it.

Hello, I place all the code hoping for your help.
I observed the entire project (3 classes) and I can't find the error.

Class Main:


Class DirChooser:


Class CreateDirectory:


I don't think there is a stream open and not closed..
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've not time to look into your code in any depth but I have noticed in your createDirectory() method you open a FileInputStream for every file in the song array and don't close any of them. This may or may not be your problem but either way it needs fixing, if it doesn't solve your problem look through your code for similar issues.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony is right. In fact you should be in the habit of closing every reader, writer, input stream, output stream, scanner, formatter etc in a finally so as to make sure it is closed.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . closing every reader, writer, input stream, output stream, scanner, formatter etc
The exceptions are
  • 1: It is not necessary to close a Scanner pointing to a String.
  • 2: You must never never close anything pointing to System.in, System.out or System.err.
  • I also wrote: in a finally so as to make sure it is closed.
    Of course we are all using Java7 or Java8 so you would use try‑with‑resources instead.
     
    Alicia Perry
    Ranch Hand
    Posts: 66
    Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks to all. I've solved!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!