The moose likes Java in General and the fly likes why the files are not deleted and not renamed? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

why the files are not deleted and not renamed?

damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

here is my code , the problem is the files are not deleted and not renamed.

filePath="c:/abc.txt"

this filePath will be passed as parameter from another method by calling this method

public viod savefiles(OutputStream saveStream String filePath)throws Exception
{
try{
FileOutputStream fos = new FileOutputStream(filePath);
saveStream.writeTo(fos);
fos.flush();
fos.close();
String renamefilename="";
int filelength=filePath.length();
renamefilename=filePath.subString(0,filelength-7);
renamefilename=renamefilename+"efg.txt";
File f1 = new File(filePath);
File f2 = new File(renamefilename);
if(f1.exsist())
{
f1.delete();
}
if(f2.exsist())
{
f2.renameTo(filePath);
}
}
catch(Exception e){
e.printStackTrace();
}
}
[ June 07, 2008: Message edited by: Bear Bibeault ]

<a href="http://stackoverflow.com/users/668970/user668970" rel="nofollow">
<img src="http://stackoverflow.com/users/flair/668970.png" >
</a>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13411

Well, you tell us. Your code catches the exception, and prints out the stack -- what does the exception (and trace) say is the cause?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

my code says that

1) it will get the data from outputstream and wrrite to the file as c:/abc.txt,

2) file file f1 exsist it will delete that file and create new file f2 with name c:/efg.txt and rename that efg.txt as abc.txt
damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

my code says that

1) it will get the data from outputstream and wrrite to the file as c:/abc.txt,

2) file file f1 exsist it will delete that file and create new file f2 with name c:/efg.txt and rename that efg.txt as abc.txt

3) Please dnt concisder for what exception it throws.

The Problem is even if file f1 is there it not deleted and f2 is not renamed to f1
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13411

3) Please dnt concisder for what exception it throws.

The Problem is even if file f1 is there it not deleted and f2 is not renamed to f1


And why is the exception not important? For all we know, you could have a null pointer exception on the first line of the try block -- and the none of the delete or rename code is even executed.

Henry
damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

hi Henry Wong,

Null pointer exception


will please explain clearly and if possible suggest me ......


redards

DAM
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18641
Um, Henry seems to be assuming that an exception is thrown. If that's the case, then absolutely, report its error message and stack trace. However the methods delete() and renameTo() were written by perverse programmers who didn't feel the need to throw any exception in case of failure - they just return false. I consider it an embarrassment that Java has not provided better methods than these, in all this time.

Damodar, there are several possible reasons why these might fail:
  • Another stream or program may still be open for that file - be sure that streams are closed in a finally block before you try to delete.
  • One of the paths may be incorrect
  • You may not have permission to delete or rename that file.
  • The file may have already been deleted or renamed.

  • Unfortunately there's not really a good way to know which is the problem, other than checking each possibility carefully.


    "I'm not back." - Bill Harding, Twister
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 13411

    Originally posted by damodar kumar:

    will please explain clearly and if possible suggest me ......


    The stacktrace gives you the exact error -- down to the exact line of code. If you give us the trace output, maybe we can point you to the right place to look...

    Henry
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 13411

    Henry seems to be assuming that an exception is thrown


    Actually, I didn't assume that an exception was thrown. I was just confused that anyone wouldn't even consider it as valueable to examine, if one was thrown -- which was hinted by the exchange.

    [EDIT: Hmmmmm... maybe I did initially assumed it... ]

    Henry
    [ June 07, 2008: Message edited by: Henry Wong ]
    damodar kumar
    Ranch Hand

    Joined: May 19, 2008
    Posts: 77

    henry

    i dnt find any exception in the above....
    damodar kumar
    Ranch Hand

    Joined: May 19, 2008
    Posts: 77

    Hi Jim ,

    Thanks for your valuable suggestions.

    according to the points you mentioned in the post. I once again gone through the code again.

    1) for the first point, i am closing the stream before file is being deleted.

    2)This is regarding file permission, as this is dynamically generated files so, i think may not be created in protected manner.

    3) Path of the file is correct.

    4)the file was not deleted previously.

    and even i am not getting any exception but it is returning false for both delete() and rename to() also

    wait for your valuable suggestions.


    Thanks,

    Dam.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 13411

    Can you post actual working code? Preferrably one that has a test method too?

    Your code has so many compile errors -- and after 10 minutes of trying to fix them, I had no idea what to do with this...



    This method doesn't exist in the output stream class. And for the methods that are close in name, none of them takes a file output stream.

    Henry
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 13411

    And BTW, this....



    was annoying to decipher. Besides the obvious that the method doesn't exist due to the wrong case -- it is the substring() method. Once it was fixed, it was still confusing, as I had a different test filename. This only works if your filename was exactly 7 in length. (BTW, I assumed that the purpose of this was to get the drive letter)

    Henry
    damodar kumar
    Ranch Hand

    Joined: May 19, 2008
    Posts: 77

    HI Wong

    here im posting the code again,

    public void saveFile(ByteArrayOutputStream txtOutputStream,
    String txtOutputFileWithPath) throws FormsTemplatesRuntimeException {
    final String METHOD_NAME = "savetxtFile ::";
    FileOutputStream fos = null;
    int pathlength = 0;
    String encryptFile = "";

    try {
    if ((null != txtOutputFileWithPath) &&
    !(txtOutputFileWithPath.equals(""))) {
    fos = new FileOutputStream(txtOutputFileWithPath);
    txtOutputStream.writeTo(fos);
    txtOutputStream.flush();
    txtOutputStream.close();

    //renameFile=txtOutputFileWithPath;
    if (null != txtOutputStream) {
    File renameFile = new File(txtOutputFileWithPath);

    if ((null != txtOutputFileWithPath) &&
    !txtOutputFileWithPath.equals("")) {
    pathlength = txtOutputFileWithPath.length();
    encryptFile = txtOutputFileWithPath.substring(0,
    pathlength - 4) + "_1.txt";
    File file1 = new File(txtOutputFileWithPath);

    File file2 = new File(encryptFile);

    file1.exists());
    if (file1.exists()) {
    file1.delete();

    }

    if (file2.exists()) {
    file2.renameTo(file1);

    }
    }
    }
    }
    } catch (Exception exp) {

    FTREx.printStackLog();
    throw FTREx;
    } finally {
    try {
    if (null != fos) {
    fos.flush();
    fos.close();
    fos = null;
    }

    if (null != txtOutputStream) {
    txtOutputStream.flush();
    txtOutputStream.close();
    txtOutputStream = null;
    }
    } catch (IOException ioexp) {

    }
    }
    }
    Rob Spoor
    Saloon Keeper

    Joined: Oct 27, 2005
    Posts: 17259

    damodar, please UseCodeTags. You can edit your post to add these tags using the button.


    SCJP 1.4 - SCJP 6 - SCWCD 5
    How To Ask Questions How To Answer Questions
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18641
    Apparently, continued here.
     
     
    subject: why the files are not deleted and not renamed?
     
    WebSphere development made easy
    without the weight of IBM tools
    http://www.myeclipseide.com