<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks
    http://joust.kano.net/weblog/archives/000071.html

    Code to safely delete a directory containing symbolic links

    Today I had to write code to delete a directory, which, in Java, requires individually deleting all of the files in it as well as its subdirectories and their subdirectories and so on.

    It's easy to recursively delete things, but if your code is going to run on a system with symbolic links (Linux, Unix, OSX (?)) you need to be careful. The code I came up with is below:
    (This code is released in the public domain. Feel free to use it in your own project.)
    
    public static boolean deleteDir(File dir) {
    // to see if this directory is actually a symbolic link to a directory,
    // we want to get its canonical path - that is, we follow the link to
    // the file it's actually linked to
    File candir;
    try {
    candir = dir.getCanonicalFile();
    } catch (IOException e) {
    return false;
    }
    // a symbolic link has a different canonical path than its actual path,
    // unless it's a link to itself
    if (!candir.equals(dir.getAbsoluteFile())) {
    // this file is a symbolic link, and there's no reason for us to
    // follow it, because then we might be deleting something outside of
    // the directory we were told to delete
    return false;
    }
    // now we go through all of the files and subdirectories in the
    // directory and delete them one by one
    File[] files = candir.listFiles();
    if (files != null) {
    for (int i = 0; i < files.length; i++) {
    File file = files[i];
    // in case this directory is actually a symbolic link, or it's
    // empty, we want to try to delete the link before we try
    // anything
    boolean deleted = file.delete();
    if (!deleted) {
    // deleting the file failed, so maybe it's a non-empty
    // directory
    if (file.isDirectory()) deleteDir(file);
    // otherwise, there's nothing else we can do
    }
    }
    }
    // now that we tried to clear the directory out, we can try to delete it
    // again
    return dir.delete();
    }
    
    Posted by keith at February 5, 2004 12:08 AM | TrackBack



    ===================================

    http://www.eddie-meyer.com/docs/notes/delete-symbolic-link-from-java.html

    Deleting a symbolic link

    Given a symbolic link of

    	fileA -> fileB
    

    what happens if you try to call file.delete() (the Java 'delete' command) on fileA?

    To test what happens, I wrote a test program using Java1.4 on Sun Solaris 8.

    Here is the program...

    /* Program to test what happens if you try to delete a symbolic
    * link from java (running on Solaris)
    *
    * Command line arguments:
    * 		filename - the symbolic link to delete. (target can be a file or a directory.)
    */
    import java.io.*;
    public class delete
    {
    public static void main(String args[])
    {
    if ( args.length <= 0 )
    {
    System.out.println("You didn't specify the symbolic link to delete.");
    System.out.println("Exiting...");
    System.exit(1);
    }
    String link_name = args[0];
    System.out.println("Input Details...");
    System.out.println("Symbolic Link: "+link_name);
    System.out.println("");
    delete d = new delete(link_name);
    }
    /**
    * Constructor
    */
    public delete(String link_name)
    {
    if ( ! exists( link_name ) )
    {
    System.out.println( link_name + " does not exist.");
    System.out.println("Exiting...");
    System.exit(1);
    }
    if ( ! isSymbolicLink( link_name ) )
    {
    System.out.println( link_name + " is not a symbolic link.");
    System.out.println("Exiting...");
    System.exit(1);
    }
    if ( delete(link_name) )
    {
    System.out.println("Java's File.delete() returned OK.");
    }
    else
    {
    System.out.println("Java's File.delete() did NOT work OK.");
    }
    }
    private boolean exists(String link_name)
    {
    File f = new File( link_name );
    return f.exists();
    }
    private boolean isSymbolicLink(String link_name)
    {
    try
    {
    File f = new File( link_name );
    if ( ! f.getAbsolutePath().equals( f.getCanonicalPath() ) )
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    catch(IOException e)
    {
    System.out.println("Exception occurred while trying to determine if " + link_name + "is a symbolic link.");
    System.out.println("Can't continue, exiting...");
    System.exit(1);
    return false;
    }
    }
    private boolean delete(String link_name)
    {
    File f = new File(link_name);
    return f.delete();
    }
    } // end of class.
    

    The result of my tests were that the symbolic link gets removed (whether the symbolic link points to a file or a directory) but the target object was left untouched.

    Note: In the above code, I attempted to identify a symbolic link by comparing the file's absolute path to it's canonical path (see the 'isSymbolicLink(...)' method above). This was the method I had seen suggested several times on the web. Since implementing this method, however, I have determined that it is NOT reliable enough and I am not all that happy with it... (see the following thread in the Java secion of the forums area).

    posted on 2007-04-17 14:36 jinfeng_wang 閱讀(517) 評論(0)  編輯  收藏 所屬分類: java 、ZZ
    主站蜘蛛池模板: 亚洲日韩在线视频| 国产亚洲一区区二区在线 | 18女人水真多免费高清毛片| 亚洲热线99精品视频| 午夜成人无码福利免费视频| 国产三级电影免费观看| 亚洲欧美在线x视频| 日日操夜夜操免费视频 | 搜日本一区二区三区免费高清视频 | 亚洲神级电影国语版| 99热在线精品免费播放6| 亚洲综合视频在线| 成人免费大片免费观看网站| 亚洲午夜一区二区电影院| 免费无码肉片在线观看| 亚洲欧美日韩中文无线码| 日本高清免费aaaaa大片视频| 亚洲av永久无码精品秋霞电影秋| 免费看的黄色大片| 婷婷亚洲综合五月天小说在线| 啊v在线免费观看| 波霸在线精品视频免费观看| 亚洲va久久久噜噜噜久久天堂| 最近中文字幕国语免费完整| 亚洲妇女熟BBW| 免费人成在线观看网站品爱网日本| 免费一区二区三区在线视频| 亚洲日韩精品一区二区三区无码 | 人妻丰满熟妇无码区免费| 亚洲a∨无码男人的天堂| 日韩免费无码一区二区视频| 国产免费一区二区三区免费视频| 亚洲av日韩av不卡在线观看| 97无码免费人妻超级碰碰夜夜| 免费一级毛片在线播放放视频 | 亚洲成av人无码亚洲成av人| 亚洲片一区二区三区| 91香焦国产线观看看免费| 亚洲国产日韩a在线播放| 亚洲人成网站色在线入口| 少妇太爽了在线观看免费视频|