Cannot delete file ...bug?

This appears like a bug to me...see if you agree.
A file opened for read-only access and memory mapped using the map method of FileChannel cannot be deleted even when the channel is closed. An example of this is the simple Grep.java example from NIO modified only to try and delete the file. On Win2K, the delete fails. Once the channel is closed, it should be able to delete the file. Is this a bug?
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.regex.*; public class Grep {     // Charset and decoder for ISO-8859-15
private static Charset charset = Charset.forName("ISO-8859-15");
private static CharsetDecoder decoder = charset.newDecoder(); // Pattern used to parse lines
private static Pattern linePattern
= Pattern.compile(".*\r?\n"); // The input pattern that we're looking for
private static Pattern pattern; // Compile the pattern from the command line
private static void compile(String pat) {
try {
pattern = Pattern.compile(pat);
} catch (PatternSyntaxException x) {
System.err.println(x.getMessage());
System.exit(1);
} // Use the linePattern to break the given CharBuffer into lines, applying
// the input pattern to each line to see if we have a match
private static void grep(File f, CharBuffer cb) {
Matcher lm = linePattern.matcher(cb);// Line matcher
Matcher pm = null;// Pattern matcher
int lines = 0;
while (lm.find()) {
lines++;
CharSequence cs = lm.group(); // The current line
if (pm == null)
pm = pattern.matcher(cs);
else
pm.reset(cs);
if (pm.find())
System.out.print(f + ":" + lines + ":" + cs);
if (lm.end() == cb.limit())
break;
} // Search for occurrences of the input pattern in the given file
private static void grep(File f) throws IOException { // Open the file and then get a channel from the stream
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory
int sz = (int)fc.size();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer
CharBuffer cb = decoder.decode(bb); // Perform the search
grep(f, cb); // Close the channel and the stream
fc.close();
// Try deleting the file =================================
boolean deleted = f.delete();
if (!(deleted)) {
System.err.println("Could not delete file " + f.getName());
System.exit(4);
// End try deleting file =================================
} public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage: java Grep pattern file...");
return;
compile(args[0]);
for (int i = 1; i < args.length; i++) {
File f = new File(args);
try {
grep(f);
} catch (IOException x) {
System.err.println(f + ": " + x);

Here is the minimal code that demonstrates this. It opens the file specified on the command line, maps it to memory, prints it out, and then tries to delete the file.
There is no question about calling close on a File object. The close method is invoked on a stream or a channel. In the case of a channel, it should automatically close the stream. However, in this code I am closing the stream and the channel.
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class testFileDelete {
public static void main(String[] args) {
          FileInputStream fis = null;
          if (args.length < 1) {
               System.err.println("Usage: java testFileDelete <filename>");
               System.exit(1);
          File f = new File(args[0]);
try {
               // Open the file
               fis = new FileInputStream(f);
          } catch (FileNotFoundException ex) {
               System.err.println("Error! " + ex.getMessage());
               System.exit(2);
          try {
               // Get a channel from the stream
               FileChannel fc = fis.getChannel();
               // Map the file into memory
               MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
               // Do something interesting here. For this example, just print the
               // contents of the file.
               // Decode the file into a char buffer, so we can print the contents.
               Charset cs = Charset.forName("8859_1");
               CharsetDecoder cd = cs.newDecoder();
               CharBuffer cb = cd.decode(bb);
               // Now print it out to standard output
          System.out.print(cb);
               // Close the channel and the stream
               fc.close();
               // Close the input stream even though closing the
               // channel should do this
               fis.close();
          } catch (IOException ex) {
               System.err.println("Error! " + ex.getMessage());
               System.exit(3);
          // Done processing file. Now delete it.
          boolean deleted = f.delete();
          if (!(deleted)) {
               System.err.println("Could not delete file " + f.getName());
               System.exit(2);

Similar Messages

  • Cannot delete file Windows2Local/Components/{multiple}.settings

    Every time I shutdown JSE it shows a couple of ugly messages saying:
    "Cannot delete file Windows2Local/Components/classes.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    "Cannot delete file Windows2Local/Components/sources.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    "Cannot delete file Windows2Local/Components/watches.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    "Cannot delete file Windows2Local/Components/properties.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    "Cannot delete file Windows2Local/Components/designpattern.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    "Cannot delete file Windows2Local/Components/threads.settings in C:\Documents and Settings\Joost\.jstudio\Ent8\config"
    Assuming that JSE had some problems to access my profile (oh, how I *hate* Windows...) I wanted to change the JSE working directory to C:\Projects\Java Studio Enterprise, but I can't find the place within the Tools|Options where I can set that. Help!
    Regards,
    Joost

    Thanks for the reply. I added the -userdir option, and now it complains:
    "Cannot delete file Windows2Local/Components/ComponentPalette.settings" in C:\Projects\Java Studio Enterprise\config"
    "Cannot delete file Windows2Local/Components/CollaborationExplorer.settings" in C:\Projects\Java Studio Enterprise\config"
    Hmmm, it seems to me that - although the number of error messages has been reduced - JSE is still not very happy about something. It would be nice if it was a bit more precise in its problem description.
    Regards,
    Joost

  • Cannot delete files from my ipod shuffle!!!

    I am new to using an ipod and i cannot delete files from my ipod, on itunes, it only shows the license agreement where the ipod files are supposed to be and no matter how many times i uninstall and reinstall itunes and agree to the license agreement i can't access anything else. can anyone help me at all i feel like a complete idiot!

    Pressing return will get past this screen as there is a button at the bottom right of iTunes which you likely cannot see.
    Set your screen resolution to at least 1024x768 to see this button.

  • Admin suddenly cannot delete files without entering password?

    Admin (and sole user) suddenly cannot delete files without entering password?

    Admin (and sole user) suddenly cannot delete files without entering password?

  • Cannot delete file from external hard drive (error code 36) in Finder, but Terminal is ok

    I have a relatively new Macbook Pro Retina 15" and have a Western Digital 1 TB Passport.  I've been using the passport for about a year now without any problems on older Macbooks in my house.  I used to be able to read and write to this drive no problem.  I am using the Paragon NTFS for Mac software in order to read and write to the NTFS drive.  When I navigate through the drive in Finder, if I try to create a new folder, I do not have a problem.  If I try to delete that folder I just created, I get this error:
    The Finder can’t complete the operation because some data in “untitled folder” can’t be read or written.
    (Error code -36)
    The folder has nothing in it whatsoever.  I just created it.  But if I go to Terminal and I run
    rm -rf "untitled folder"
    This works fine.  Why is Terminal acting different than Finder?  This does not happen on my other Macbooks.  Thanks in advance.

    Thanx for such qucik reply!!! The pages were great, it had an answer for my problem, but unfortunately it also involved me dealing with it only on the MacIntosh HD itself, not on mounted external drive I suppose... So it didn't work they say that there is an icon in the left corner indicating locked folder - I don't see any icon on it. It says you can untick the box to unlock the folder - there is no tick to be unticked. Finally, they say that if I don't have permition or rights I cannot delete it. But how do i change permittion which already says that i can read and write in it? So everything appears to be normal only I cannot delete it. there is not even any warnig telling me I don't have enough rights, it just requires the password and after me typing it in it makes the sound of scraping paper but does nothing more. I aslo had some problems with emptying Trash after being seemingly succesful with one file and had got so far to have them appear in the Trash (no clue how this one actully got there though..) I followd the advice and emptied the Trash with Terminal. The Folder is still there but it shows 0 Kb... So far so good BUT: It is still on the external drive, so I obviously only managed to copy it into the Trash!!! Phew, innit a bit too tight to poor owners, this macbook?? I want my rights back!

  • Cannot delete file from disk after emailling that file like attachment

    After i send some file from disk in Mail like attachment i cannot delete that file from disk unless i quit Mail. Like Mail is still using a file. Of course, that mail is in Sent mail, not in outbox.

    This deeper than that. I created another account on the same iMac and it works in Harmony with my other devices and Documents in the cloud. I can only imagine the icloud account is somehow corrupted (I am on my macbook not iMac at the moment). There have been no updates to the Calendar, Contacts or Documents since early January on that account. I have spent 1hr on the phone with support and they were baffled as well. Only thing I can gove as insight to the problem is this, I was on the Mac Mini which was backed up on Time Machine. about 4 or 5 months ago I had to completley restore the Mac Mini becasue of the never ending circle on the load screen. The Backup failed to work it kept loading the issue and 4 or 5 times the circle of death kept coming up I would subsequently wipe the Mini and go further back in the back up until I go a copy that worked.  I then loaded the Time Mache back up into my NEW iMac and wnet from there. It took about a month for this problem I am detailing above to start happening. It maybe a corrupt file loaded from the backup into the new iMac causing this problem. Best answer I can think of on my own???

  • Re: Satellite L450D Cannot delete file from desktop

    Hi all, I was veiwing some photos on yahoo email via firefox version 3 6 3 sent by my sister in Australia, when this file appeared on my desktop which I cannot delete.
    The file is called Pez & Lisa (after my sister and brother in laws photograph) when i try to delete it I get the message "COULD NOT FIND THIS ITEM, THIS IS NO LONGER LOCATED IN C/USERS/PAUL/DESKTOP VERIFY THE ITEMS LOCATION AND TRY AGAIN"
    The file when I right click on proprties reads the file is 0 bytes size.
    It cannot be deleted in safe mode or moved or renamed or anything!
    Any ideas??

    Hi Guys, thanks for your replies, however, there is nothing in startup or task manager to delete!
    I have tried downloading unlocker, but it doesnt do anything when I click on it!! Maybe it doesnt work with Windows 7.
    I suppose I can leave it alone as its not doing anything! its not worth a reformat just do get rid of an icon.
    Ho Hum.

  • Cannot delete files after emailing them

    Ever since I upgraded to Snow Leopard I cannot empty the trash if there is a file that I have added as an attachment in an email if Mail is still open. It will say the file is in use and I either have to quit Mail or Secure Empty Trash. This is really annoying. Anyone know of a fix? The file shouldn't be in use once it is sent in Mail. This never happened in previous versions of OSX. Also, if I have sent a file as an attachment I cannot save over it in the originating program because it is in use.

    No, this is only for Mail. As soon as I add a file as an attachment then I cannot delete it without quitting mail or secure empty the trash. It isn't a data loss deterrent because you get that message making sure you want to delete it but when I say yes then it says the file is in use.
    By the way, I shut off Time Machine for a day and then tried to delete a new file that I created after Time Machine was shut off that I added as an attachment to Mail and the same problem occurs. All other files that I do not add as an attachment in Mail I can delete with no problems.

  • Cannot Delete file iCloud

    I am having an issue with the New Late 2012 iMac and syncing numbers documents in the cloud.
    1) I cannot sync a native document from the new iMac to the cloud via the Numbers Program. I can drag the document into the browser and update it that way
    2) After doing this as a work around the document devlopes an error message making it unable to drag and drop 3 times seems to be the max allowed with this method
    Spent an hour on the phone with Apple to no avail, I have tried
    1) Deleteing icloud off all my devices Mini, Iphone, Ipad macbook air and iMac.
    2) Yes I have made sure Docs in the cloud is turned on on all devices and the right icloud account is signed in
    3) rebooted everything a few times
    4) created another profile on the IMac which is able to update via the Native Numbers program and not the Browser as I was doing on profile 1.
    The problem seems to be this file which is gray'd out and I cannot delete it off any device, the date that this started happening is the creation date of this document, so process of elimination I want to delete this document to see if this is corrupting profile 1 and disabling my ability to upload files via the native Numbers program.
    Anyone have a document that is in the constant update state on icloud that cannot delete it with a new Mac? Its a very frustrating problem because I use the mac as the mother ship to handle presentations and spread sheets and its not pushing out what I do on it and its brand new..Considering doing a time machine restore on the new profile to see if that works..Anyone have some more thoughts, I have tried all the simple fixes thanks to Apple and a very nice female voice named Kerry.

    This deeper than that. I created another account on the same iMac and it works in Harmony with my other devices and Documents in the cloud. I can only imagine the icloud account is somehow corrupted (I am on my macbook not iMac at the moment). There have been no updates to the Calendar, Contacts or Documents since early January on that account. I have spent 1hr on the phone with support and they were baffled as well. Only thing I can gove as insight to the problem is this, I was on the Mac Mini which was backed up on Time Machine. about 4 or 5 months ago I had to completley restore the Mac Mini becasue of the never ending circle on the load screen. The Backup failed to work it kept loading the issue and 4 or 5 times the circle of death kept coming up I would subsequently wipe the Mini and go further back in the back up until I go a copy that worked.  I then loaded the Time Mache back up into my NEW iMac and wnet from there. It took about a month for this problem I am detailing above to start happening. It maybe a corrupt file loaded from the backup into the new iMac causing this problem. Best answer I can think of on my own???

  • Cannot delete file on windows

    WE have an application. This application processes a list of files in a directory. Once it has processed, it is deleted. We are using the File.delete() API to delete the file. This application works fine on Unix, but when run on Windows, the delete method returns false status. It cannot delete the file and neither does it throw the exception. Is there any way to find out why this delete is failing. We have no clue.

    Maybe the file you get by the new operation
    tion doesn't exist.
    Pls check the return value of the
    the File.exists();.I can't believe that
    your code failed to delete the file if the return
    value is true.sudhirkd never said delete() returned true when it shouldn't have. Regarding exists(), it returns the value that matches the file's actual existence in the file system. Please run the following code on a Windows system to check for yourself:
            java.io.File file = new File ("temp.file");
            java.io.FileWriter out = new FileWriter (file);
            System.out.println ("file.delete() returns: " + file.delete ());
            System.out.println ("file exists: " + file.exists ());
            out.close (); // THIS IS THE IMPORTANT BIT
            System.out.println ("out closed");
            System.out.println ("file.delete() returns: " + file.delete ());
            System.out.println ("file exists: " + file.exists ());it will return something like this:
    file.delete() returns: false
    file exists: true
    out closed
    file.delete() returns: true
    file exists: false
    Once again, note that it is the explicit close() of the file writer that matters here, not running System.gc(). Relying on garbage collector is a sign of very immature implementation and unprofessional programming style. Java programmers need to be as careful about releasing resources as C/C++ programmers -- just because the heap memory is handled for you automatically does not mean you could ignore file handles, temp files, socket handles, etc.
    The example applies to Windows. Explicitly closing file handles is equally important on UNIX as well. For example, failure to do so and relying on GC may cause a JVM process to run out of file handles very quickly [default max is ~20 on Solaris unless you explicitly increase the soft porcess limits].
    Vlad.

  • After Update, cannot delete files from nss volumes

    Not sure if this is the same problem caused by the SLES11 upgrade, but after running all the upgrades on an OES11 system, I can see all the nss volumes of the upgraded servers from workstations using the Novell client, and I can copy or create files on these volumes. However, I cannot delete any files, they disappear from the workstation windows explorer when they are "deleted", but an f5 promptly shows the files are still there. I can go into the server as root, and delete the files under Linux with no problems, so this part is working OK.
    I am running Novell 11.1 patch 1, and this problem has only been noticeable after the update installation.
    I have supervisor rights to the volumes and files, and if I additionally set all the other file rights, I am still unable to delete the files.
    Anyone got any ideas?
    Cheers.
    ChasR

    Originally Posted by chas
    Not sure if this is the same problem caused by the SLES11 upgrade, but after running all the upgrades on an OES11 system, I can see all the nss volumes of the upgraded servers from workstations using the Novell client, and I can copy or create files on these volumes. However, I cannot delete any files, they disappear from the workstation windows explorer when they are "deleted", but an f5 promptly shows the files are still there. I can go into the server as root, and delete the files under Linux with no problems, so this part is working OK.
    I am running Novell 11.1 patch 1, and this problem has only been noticeable after the update installation.
    I have supervisor rights to the volumes and files, and if I additionally set all the other file rights, I am still unable to delete the files.
    Anyone got any ideas?
    Cheers.
    ChasR
    I had a similar problem at updating with SP3 on SP4. It was similar, that permission ERASE is not applied to all trustees working through ncp in spite of the fact that it was available.
    Has solved a problem that has redefined this permission for trustees on all catalogues.

  • Cannot Delete Files From a Flash Drive

    I let a friend of mine borrow my flash drive so that he could do some type of system recovery or something on his microsoft surface tablet. He gave it back to me so I could use it for something I need to do, but he did not delete any of the recovery files, so I currently do not have enough memory to use it for what I need it for. I tried deleting the items and everytime I receive an error message and I cannot delete them. If any one has any ideas of what I can do to fix this I'd really appreciate it. I right clicked and went to get info to check the permissions and it said read and write was enabled so I'm not sure what else to do.

    Sweet...that totally worked. Thank you!

  • Adobe bridge CS5.5 cannot delete files

    Hi
    I have latest version of Lion, and recently upgraded CS3 to CS5.5. All my files under my home folder are there to view in bridge and were ofcourse created under CS3.
    If I select ANY file, I cannot delete it, or move it, or rename it, or do a batch rename.. Nothing, yet it I find it in my finder window, I can delete it.
    If I create a new .ai file which would be with CS5.5 I CAN delete it. So I am wondering what permissions are not sufficient from CS3 to CS5.5 in bridge. I have spoken to Apple Tech and they advised me to change my home folder permissions for Everyone to read and write and apply to enclosed items. That didn't work.
    I have heard there is an issue between Lion and Adobe, but not sure I buy that one. At the end of the day Bridge is just like a finder window, except it have more tools and more links to more adobe applications, but basically a file view window. Yet when I choose to view files in Finder I can delete them, except it will keep asking me to enter my user pword everytime with no option to remember it.
    The only way of getting around this so far is to manually one by one is SAVE AS each file with a new file name.. no auto script option I can see of doing it so far.
    I have ALOT of files, surely there is something I should be knowing after all these years.
    HELP!
    Dan

    Thank you, SG, for clarifying which OS's this will work with.
    I agree with Jennie - the beauty of "get photos from camera" is that it takes 2 steps out of the workaround you mention above. Personally, I'm okay living with 6.4.1, as I have an older camera. I won't be happy if one of the push updates re-updates me to 6.5 though. I wouldn't want to redo the reinstallation of the new version. This method will not work for any of my clients or students though - they normally can't comfortably follow the technicality of downgrading, and some have newer cameras and would need the 6.5 version. (I don't believe in betas for my clients, and neither do they, for the most part)
    I guess I should thank Adobe - some of my clients have needed tech support to perform the downgrade (SO many people aren't comfortable technically), and some of my clients needed to be retaught the workaround SG mentions. Adobe's flub has actually made me a little money - which I'd rather not have to make this way.
    SG's workaround does work and makes sense - but for my clients with hundreds of images at a time, who have to rename items, add copyright info, & etc., this more than doubles their work-time for the start of their workflow. I hope Adobe gets that GM version out soon.
    BTW - this issue supports an argument I've hadfor years regarding "PUSH" updates. I always tell my clients and students not to do them until they research the update. Because of what I do, I do have to install them personally, but I'm grateful I'm comfortable fixing things myself.

  • Cannot delete files using dreamweaver

    I just updated a site to the latest version of PHP and MySql. I find that while I can upload new files to the site, I cannot delete either existing or new files using Dreamweaver CS6.
    Has anyone else had this problem?
    Howard Walker

    Have you tried with a different FTP client?
    This sounds like a LINUX permissions/ownership issue. Upgrading PHP would not change the permissions/ownership, unless something else happened during the process, such as that the files were moved to a different location.
    The way PHP runs affects permissions/ownership (DSO, CGI, suPHP, etc.), but even if that changed I don't think it would affect the permissions set on your site files.

  • Cannot delete file(s). Do not have Permission.

    Cannot unlock files or add permissions in the Info window. We're on a network of 12 systems... Do I have to locate the original system? The system that placed the file on our server originally?

    Hi Doug,
    Do you mean the lock cannot be unlocked in the Get Info window? What are the rights now?

Maybe you are looking for

  • T43 BSOD's when connected to a Projecter

    Ok here is a good one for you guys. I have a T43 type 1875 that blue screens everytime it is plugged into a projector.  I have other similar laptops (some T43's some HP's) and they all work with the projector without any issues.  This system in parti

  • Security in ODI-specific access to projects and folders

    Is there anyway to give project or folder-specific access within ODI? Some users want to be able to run jobs-but I dont want them to see other projects within ODI. Thanks.

  • Any way to roll back to 10.9.0 from 10.9.1?

    the 10.9.1 and restart is prompting for all sorts for keychain passwords (some of which I evidently don't remember).   Is there any way to get out of this Groundhog Day loop?  Restarting just starts the prompting process (and beachball) all over agai

  • Running oracle forms and reports on the web

    can any one help me out.... I have oracle 9i database as back end and oracle forms & reports 6i which was running on client server architecture. Now i want to deploy all my forms on web so that it runs on internet explorer . For all these things to h

  • Create "One Time Vendor"

    Hi All, How can we create "One Time Vendor"?! I would to create it to add on  the payee to print cheques. Thanks