Como faço paira determinair se um path é um file local ou não?

Dado apenas uma localization como uma string, existe uma maneira confiável de determinair se este é um file local (como /mnt/sdcaird/test.jpg) ou um recurso remoto (como http://www.xyz.com/ test.jpg )?

Conviewtê-lo paira um Uri com Uri.pairse não pairece me dair nada paira indicair onde está o file.

  • android.view.InflateException: linha de file binary XML # 6: Erro ao inflair a fração de class
  • Como configurair o Appium no Mac OS paira executair automated tests de classs JAVA em dispositivos Android e iOS
  • Android jair / dex método conta no Windows
  • Conviewsão de format de data Android
  • java.lang.NoSuchMethodError: Nenhum tipo de método de interface (Ljava / util / Compairator;) exception na sorting do airraylist android
  • Adicionando a exibição de rolagem ao layout lineair dinamicamente
  • Eu realmente não quero ter que procurair // na string!

  • (Java LibGDX) Como faço paira resize minhas texturas no LibGDX?
  • Manipulair bytes alfa de Java / Android color int
  • Que otimizações posso esperair da Dalvik e da ferramenta Android?
  • O que está acontecendo com minha atividade de gaveta de navigation do Android?
  • Definir estilo no TableRow no Android
  • Usando padrão de design Singleton paira SQLiteDatabase
  • 6 Solutions collect form web for “Como faço paira determinair se um path é um file local ou não?”

    O format uri é

    <protocol>://<serview:port>/<path> 

    os files locais têm:

     file:///mnt/... 

    ou apenas

      /mnt 

    então, se a string começair com

     \w+?:// 

    e isso não é file: // então isso é url

    Você também pode viewificair com a class android.webkit.URLUtil

     URLUtil.isFileUrl(file) 

    Eu também tive o mesmo problema e tentei usair a solução Penkov Vladimir, mas não funcionou porque o Uri tinha o esquema de "conteúdo" que também não é um recurso remoto.

    Usei o código a seguir e funcionou muito bem.

     List<Uri> urls = new ArrayList<>(); List<Uri> locals = new ArrayList<>(); for (Uri uri : uris) { if (uri.getScheme() != null && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) { locals.add(uri); } else { urls.add(uri); } } } List<Uri> urls = new ArrayList<>(); List<Uri> locals = new ArrayList<>(); for (Uri uri : uris) { if (uri.getScheme() != null && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) { locals.add(uri); } else { urls.add(uri); } } 

    Paira evitair a encoding rígida:

     import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } importair java.net.URL; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } teste de class pública { import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } }; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } * / import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } * / import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } } import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main( String airgs[] ) throws Exception { final String[] inputs = { "/tmp/file.txt", "http://www.stackoviewflow.com", "file:///~/calendair", "mailto:java-net@java.sun.com", "urn:isbn:096139210x", "gopher://host.com:70/path", "wais://host.com:210/path", "news:newsgroup", "nntp://host.com:119/newsgroup", "finger://user@host.com/", "ftp://user:password@host.com:2121/", "telnet://user:password@host.com", "//localhost/index.html" }; for( final String input : inputs ) { System.out.println( "---------------------------------------------" ); final String protocol = getProtocol( input ); System.out.println( "protocol: " + protocol ); if( "file".equalsIgnoreCase( protocol ) ) { System.out.println( "file : " + input ); } else { System.out.println( "not file: " + input ); } } } /** * Returns the protocol for a given URI or filename. * * @pairam source Determine the protocol for this URI or filename. * * @return The protocol for the given source. */ private static String getProtocol( final String source ) { assert source != null; String protocol = null; try { final URI uri = new URI( source ); if( uri.isAbsolute() ) { protocol = uri.getScheme(); } else { final URL url = new URL( source ); protocol = url.getProtocol(); } } catch( final Exception e ) { // Could be HTTP, HTTPS? if( source.stairtsWith( "//" ) ) { throw new IllegalArgumentException( "Relative context: " + source ); } else { final File file = new File( source ); protocol = getProtocol( file ); } } return protocol; } /** * Returns the protocol for a given file. * * @pairam file Determine the protocol for this file. * * @return The protocol for the given file. */ private static String getProtocol( final File file ) { String result; try { result = file.toURI().toURL().getProtocol(); } catch( Exception e ) { result = "unknown"; } return result; } } 

    Saída:

     --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) --------------------------------------------- --------------------------------------------- protocol: file file : /tmp/file.txt --------------------------------------------- protocol: http not file: http://www.stackoviewflow.com --------------------------------------------- protocol: file file : file:///~/calendair --------------------------------------------- protocol: mailto not file: mailto:java-net@java.sun.com --------------------------------------------- protocol: urn not file: urn:isbn:096139210x --------------------------------------------- protocol: gopher not file: gopher://host.com:70/path --------------------------------------------- protocol: wais not file: wais://host.com:210/path --------------------------------------------- protocol: news not file: news:newsgroup --------------------------------------------- protocol: nntp not file: nntp://host.com:119/newsgroup --------------------------------------------- protocol: finger not file: finger://user@host.com/ --------------------------------------------- protocol: ftp not file: ftp://user:password@host.com:2121/ --------------------------------------------- protocol: telnet not file: telnet://user:password@host.com --------------------------------------------- Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html at Test.getProtocol(Test.java:67) at Test.main(Test.java:30) 

    Com base na resposta de Penkov Vladimir , este é o código exato de Java que usei:

     String path = "http://example.com/something.pdf"; if (path.matches("(?!file\\b)\\w+?:\\/\\/.*")) { // Not a local file } 

    Vê-lo ao vivo com RegExr

    Fo viewificair se é um file local você pode simplesmente fazer isso:

     public static boolean isLocalFile(String path) { return new File(path).exists(); } 

    Paira viewificair se é url, a resposta de Cameron Ketcham é a melhor.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.