Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving File From One Directroy to Another

 
amogh deshpande
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Below is the code for moving file from one directory to another
and its working very fine when i run it as separate class.
but if i am trying to call the method movefile throgh run method of the thread ,it is returning false value since reName method which is used here returns boolean value..
i am stuked becoz of this.Its giving prb only if i am calling this method (movefile)throgh thread.
so if any body can help me out..

Thanks a lot?
Below is the code for moving fiel from one directory to another...


//******************************************************************
package com.bj;

import java.io.*;

public class Move
{


public void moveFile(String src)
{
String ss = src;
File file = new File(ss);
System.out.println("+++++++++++++++++++++"+file);
// Destination directory
File dir = new File("C://output//");
System.out.println(dir);
// Move file to new directory
File gg = new File(dir, file.getName());
System.out.println("||||||||||||||||||||"+gg);
try
{
boolean success = file.renameTo(gg);
System.out.println(success);
}
catch(SecurityException ex )
{
System.out.println("Exception while Renaming File" +ex);
}



}
}
//**************************************************************************
and this is the code from where i am calling method
//*******************************************************
package com.bj;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.FileReader;

public class Polling extends Thread{
//private Store store;
//int fint;


public void run()
{

Move mv = new Move();
mv.moveFile(s1);

}


//*******************************************************
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are those double forward-slashes supposed to do? On Windows, you should use back-slash, which requires a double back-slash in a Java String literal. You can get away with a single forward slash most of the time, too.

Also, does the "output" directory exist?
 
amogh deshpande
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
What are those double forward-slashes supposed to do? On Windows, you should use back-slash, which requires a double back-slash in a Java String literal. You can get away with a single forward slash most of the time, too.

Also, does the "output" directory exist?


Yes Output Directory is there.ya i have tried with \\ also..
i am calling start()
from run ()
i dont know is there any prb coz of this

Thank you!
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.renameTo will likely fail when moving from one disk partition to another (e.g. from C: do D . It can also fail when destination already exists and it will obviously fail when source file does not exist or is the user does not have required access rights.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic