Java URI from toUri(File folder)

Here you can find the source of toUri(File folder)

Description

Translates platform specific path to independent form as URI

License

Open Source License

Parameter

Parameter Description
folderpath platform specific path as <pre>UNIX: /tmp/imports/</pre> or <pre>MS Win: c:\imports</pre> or <pre>UNC MS Win: \\laptop\My Documents\</pre> are valid options

Return

an abstract path

Declaration

static URI toUri(File folder) 

Method Source Code


//package com.java2s;
/*//from  w ww  .  j  a  v  a2  s.  co  m
 * Copyright (C) 2011 Jan Pokorsky
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.net.URI;

public class Main {
    /**
     * Translates platform specific path to independent form as URI
     *
     * @param folderpath platform specific path as <pre>UNIX: /tmp/imports/</pre>
     *      or <pre>MS Win: c:\imports</pre> or <pre>UNC MS Win: \\laptop\My Documents\</pre>
     *      are valid options
     * @return an abstract path
     */
    static URI toUri(File folder) {
        if (folder.exists() && !folder.isDirectory()) {
            throw new IllegalArgumentException("Invalid folder path: '" + folder + '\'');
        }
        // File.toURI always terminates folder path with slash but the folder must exists
        return folder.toURI().normalize();
    }
}

Related

  1. toURI(CharSequence uriStr)
  2. toURI(File f)
  3. toURI(final java.io.File file, final StringBuilder builder)
  4. toURI(final String location)
  5. toURI(final String path)
  6. toURI(final String uriString)