Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35194

Re: A little help with some logic

From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: A little help with some logic
Date 2016-08-04 16:44 -0700
Organization A noiseless patient Spider
Message-ID <no0k1b$scf$1@dont-email.me> (permalink)
References <nnugtj$78m$1@dont-email.me> <nnvfu8$vov$1@dont-email.me>

Show all headers | View raw


On 8/4/2016 06:28, Eric Sosman wrote:
>     (This checks in the first order you mention, so I guess the
> second order was an "it is too late" slip-up.)
>
>     It seems clear enough to me.  All I'd suggest is that an
> exists() test might be a good idea in the File case, and that
> something more severe than printStackTrace() is probably called
> for if URISyntaxException gets thrown.
>
>     One other item that might or might not interest you: I've seen
> that File's toURI() sometimes omits the "authority" part, giving
> the unusable "file:/C:/foo.dat" instead of "file:///C:/foo.dat".
> When I first ran into this I got around it with some heavy-handed
> string-bashing, but (says the JavaDoc) the java.nio.Path{,s} classes
> may offer a better way.  Haven't tried them myself (for this purpose).
>

Thanks Eric and Stefan for looking at it.  Here's what I ended up with

         try {
             URI uri = null;
             URL url = getClass().getResource(source);
             if (url != null)
                 uri = url.toURI();
             if (uri == null) {
                 File file = new File(source);
                 if (file.exists())
                     uri = file.toURI();
             }
             if (uri == null)
                 uri = new URL(source).toURI();

             // use the URI
         } catch (URISyntaxException|MalformedURLException ex) {
             // deal with the exceptions
         }


-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A little help with some logic Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-03 21:39 -0700
  Re: A little help with some logic Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-04 09:28 -0400
    Re: A little help with some logic Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-04 16:44 -0700
      Re: A little help with some logic Robert Klemme <shortcutter@googlemail.com> - 2016-08-06 11:13 +0200

csiph-web