Bookmark Topic Watch Topic
  • New Topic

why the files are not deleted and not renamed?  RSS feed

 
damodar kumar
Ranch Hand
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
damodar kumar
Ranch Hand
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi Henry Wong,

Null pointer exception


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


redards

DAM
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 77
    Android Chrome MyEclipse IDE
    • Mark post as helpful
    • send pies
    • Report post to moderator
    henry

    i dnt find any exception in the above....
     
    damodar kumar
    Ranch Hand
    Posts: 77
    Android Chrome MyEclipse IDE
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 77
    Android Chrome MyEclipse IDE
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Report post to moderator
    damodar, please UseCodeTags. You can edit your post to add these tags using the button.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Apparently, continued here.
     
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!