0

I need to rename the file which comes without extension, then i need to pass the the renamed file(with extension). But after renaming when im assigning the that object to new file object still i'm getting the old file object(file without extension) see below code

srcFileName = tempFile.getName();

file = new File(uploadDir + srcFileName);

indexF = srcFileName.lastIndexOf(".xml");

extFile = srcFileName.substring(indexF + 1, indexF + 4);

if (!extFile.equalsIgnoreCase("zip"))
{
  if (indexF == -1)
  {
    extFile = "xml";
    srcFileName = srcFileName + "." + extFile;
    boolean rename = file.renameTo(new File(uploadDir + srcFileName));

    File renamedFile = new File(uploadDir + srcFileName);
    indexF = srcFileName.lastIndexOf(".xml");
    extFile = srcFileName.substring(indexF + 1, indexF + 4);
    long len = 0;
    len = renamedFile.length();
    debug("renamed file Length::" + len);
    //tempFile=renamedFile;

    allDetailsMap.put("listOfFiles", renamedFile);
  }
}
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
Naveen
  • 403
  • 1
  • 6
  • 20
  • 1
    Please take more care when formatting your posts - look at the preview before you submit the post, and ask yourself whether that's really how you'd want to see it if you were considering answering the question. – Jon Skeet Jan 23 '15 at 10:32
  • How do you know that your renaming function is actually working? also, your code is incomplete - where is the rest of the code? what is `file`? – ha9u63a7 Jan 23 '15 at 10:39
  • sorry.A naive stackoverflow user. what i want to do is. there is a file in the directory. im renaming that file. Then i want to assign that renamed file object to another file reference. Then i pass that reference to a hashmap for later processing. in my case , file is getting renamed but when i assign that renamed file reference to another file reference im still getting old file object i.e., file before renaming. – Naveen Jan 23 '15 at 11:03

1 Answers1

1

I am going with how I would've checked things as you have not printed any of your outputs regarding your file path changes, verification etc.

Assuming your rename operation was successful i.e. boolean rename evaluated to true, our file constructor should be:

 File renamedFile = new File(file, (uploadDir + srcFileName));

If you look at JDK documentation for File constructor:

    public File(File parent,
    String child)

Creates a new File instance from a parent abstract pathname and a child pathname string.

If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.

Otherwise the parent abstract pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty abstract pathname then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.

Parameters:
    parent - The parent abstract pathname
    child - The child pathname string

Also, an excerpt from the renameTo area (I'm sure you'v done this already):

The return value should always be checked to make sure that the rename operation was successful. 

Also, you have to confirm that your file is renamed by using getAbsolutePath() to see if the last bit contains the extension. A good example is here - What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?.

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108