Android File Path Get getRoot(File file)

Here you can find the source of getRoot(File file)

Description

Returns the filesystem root for file.

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if file is null, or non-existent
SecurityException if a security manager exists and denies read access to file
IOException if an I/O problem occurs

Declaration

public static File getRoot(File file) throws IllegalArgumentException,
        SecurityException, IOException 

Method Source Code

/*/*  w  w w . j  ava2 s  . c om*/
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Returns the filesystem root for file.
     * Specifically, this method traces back parent Files until there are no more, and returns the last one found.
     * <p>
     * <i>Note: unlike almost all other methods in this class, file need not be a normal file;</i>
     * it could be a directory as well.
     * Corollary: the result is file itself if file is a filesystem root.
     * <p>
     * @throws IllegalArgumentException if file is null, or non-existent
     * @throws SecurityException if a security manager exists and denies read access to file
     * @throws IOException if an I/O problem occurs
     */
    public static File getRoot(File file) throws IllegalArgumentException,
            SecurityException, IOException {
        Check.arg().notNull(file);
        if (!file.exists())
            throw new IllegalArgumentException("file = " + file.getPath()
                    + " is a non-existent path");

        file = file.getCanonicalFile(); // CRITICAL: if file is a relative path like ./ then the getParentFile call below will return null prematurely; cure this by putting file into canonical form
        for (File parent = file.getParentFile(); parent != null; parent = file
                .getParentFile()) {
            file = parent;
        }
        return file; // file is now the last non-null File in the chain explored by the loop above
    }
}

Related

  1. getCanonicalFile(File file)
  2. getDrive(File file)
  3. getPathLastIndex(String fileName)
  4. getPathLastIndex(String fileName, int fromIndex)