java.nio.file.Path.toUri()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中java.nio.file.Path.toUri()方法的一些代码示例,展示了Path.toUri()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Path.toUri()方法的具体详情如下:
包路径:java.nio.file.Path
类名称:Path
方法名:toUri

Path.toUri介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns a URL for the underlying file.
 * @see java.nio.file.Path#toUri()
 * @see java.net.URI#toURL()
 */
@Override
public URL getURL() throws IOException {
  return this.path.toUri().toURL();
}

代码示例来源:origin: Swagger2Markup/swagger2markup

/**
 * Convert an URI without a scheme to a file scheme.
 *
 * @param uri the source URI
 * @return the converted URI
 */
public static URI convertUriWithoutSchemeToFileScheme(URI uri) {
  if (uri.getScheme() == null) {
    return Paths.get(uri.getPath()).toUri();
  }
  return uri;
}

代码示例来源:origin: prestodb/presto

private static List<URL> getUrls(String path)
    throws MalformedURLException
{
  ImmutableList.Builder<URL> urlList = ImmutableList.builder();
  File driverPath = new File(path);
  if (!driverPath.isDirectory()) {
    urlList.add(Paths.get(path).toUri().toURL());
    return urlList.build();
  }
  File[] files = driverPath.listFiles((dir, name) -> {
    return name.endsWith(".jar");
  });
  if (files == null) {
    return urlList.build();
  }
  for (File file : files) {
    // Does not handle nested directories
    if (file.isDirectory()) {
      continue;
    }
    urlList.add(Paths.get(file.getAbsolutePath()).toUri().toURL());
  }
  return urlList.build();
}

代码示例来源:origin: twosigma/beakerx

public void addJars(List<String> dirs) {
 for (String dir : dirs) {
  try {
   myloader.addJar(Paths.get(dir).toUri().toURL());
  } catch (MalformedURLException e) {
   logger.error(e.getMessage());
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns a URL for the underlying file.
 * @see java.io.File#toURI()
 */
@Override
public URL getURL() throws IOException {
  return (this.file != null ? this.file.toURI().toURL() : this.filePath.toUri().toURL());
}

代码示例来源:origin: sleekbyte/tailor

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) {
  Path relative = Paths.get(base.relativize(dir.toUri()).getPath());
  if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) {
    return FileVisitResult.SKIP_SUBTREE;
  }
  if (isParentIncluded.getOrDefault(dir.getParent(), false)
    || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) {
    isParentIncluded.put(dir, true);
    return FileVisitResult.CONTINUE;
  }
  return FileVisitResult.CONTINUE;
}

代码示例来源:origin: twosigma/beakerx

private URL toURL(String s) throws MalformedURLException {

  s = s.trim();
  URL url = null;
  if (!s.startsWith("jar:") && !s.startsWith("http:") && !s.startsWith("https:")) {
   try {
    url = Paths.get(s).toUri().toURL();
   } catch (Exception e) {
    logger.error(e.getMessage());
   }

  }
  if (url == null) {
   url = new URL(s);
  }

  return url;

 }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * This implementation returns a URL for the underlying file.
 * @see java.nio.file.Path#toUri()
 * @see java.net.URI#toURL()
 */
@Override
public URL getURL() throws IOException {
  return this.path.toUri().toURL();
}

代码示例来源:origin: sleekbyte/tailor

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
  Path relative = Paths.get(base.relativize(file.toUri()).getPath());
  if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) {
    return FileVisitResult.CONTINUE;
  }
  if ((isParentIncluded.getOrDefault(file.getParent(), false)
    || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative)))
    && (file.toFile().getCanonicalPath().endsWith(".swift") && file.toFile().canRead())) {
    fileNames.add(file.toFile().getCanonicalPath());
  }
  return FileVisitResult.CONTINUE;
}

代码示例来源:origin: apache/hbase

private List<URL> buildClasspath(List<String> jars) throws IOException {
 List<URL> urls = new ArrayList<>();
 for (String jar : jars) {
  Path jarPath = Paths.get(jar);
  if (Files.isDirectory(jarPath)) {
   try (Stream<Path> stream = Files.list(jarPath)) {
    List<Path> files = stream
      .filter((path) -> Files.isRegularFile(path))
      .collect(Collectors.toList());
    for (Path file : files) {
     URL url = file.toUri().toURL();
     urls.add(url);
    }
   }
  } else {
   URL url = jarPath.toUri().toURL();
   urls.add(url);
  }
 }
 return urls;
}

代码示例来源:origin: robolectric/robolectric

private URL asUrl(Path path) {
 try {
  return path.toUri().toURL();
 } catch (MalformedURLException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: wildfly/wildfly

static URI propertyUrlToUri(String wildFlyConfig) {
  if (File.separator.equals("\\") && wildFlyConfig.contains("\\")) { // we are on the windows and path is for windows
    File f = new File(wildFlyConfig);
    return f.toPath().toUri();
  } else {
    try {
      URI uri = new URI(wildFlyConfig);
      if (!uri.isAbsolute()) { // URI does not include schema
        if (uri.getPath().charAt(0) != File.separatorChar && uri.getPath().charAt(0) != '/') { // relative path
          String userDir = System.getProperty("user.dir").replace(File.separatorChar, '/');
          return Paths.get(userDir, uri.getPath()).toUri();
        } else { // absolute path
          return Paths.get(uri.getPath()).toUri();
        }
      }
      return uri;
    } catch (URISyntaxException e) {
      // no config file there
      return null;
    }
  }
}

代码示例来源:origin: spotbugs/spotbugs

public JrtfsCodeBase(ICodeBaseLocator codeBaseLocator, @Nonnull String fileName) {
  super(codeBaseLocator);
  this.fileName = fileName;
  URL url;
  try {
    url = Paths.get(fileName).toUri().toURL();
    URLClassLoader loader = new URLClassLoader(new URL[] { url });
    fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap(), loader);
    root = fs.getPath("modules");
    packageToModuleMap = createPackageToModuleMap(fs);
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * This implementation returns a URL for the underlying file.
 * @see java.io.File#toURI()
 */
@Override
public URL getURL() throws IOException {
  return (this.file != null ? this.file.toURI().toURL() : this.filePath.toUri().toURL());
}

代码示例来源:origin: apache/storm

protected void fillHadoopConfiguration(Map topologyConf, String configKey, Configuration configuration) {
  Map<String, Object> config = (Map<String, Object>) topologyConf.get(configKey);
  LOG.info("TopoConf {}, got config {}, for configKey {}", ConfigUtils.maskPasswords(topologyConf),
      ConfigUtils.maskPasswords(config), configKey);
  if (config != null) {
    List<String> resourcesToLoad = new ArrayList<>();
    for (Map.Entry<String, Object> entry : config.entrySet()) {
      if (entry.getKey().equals(CONFIG_KEY_RESOURCES)) {
        resourcesToLoad.addAll((List<String>) entry.getValue());
      } else {
        configuration.set(entry.getKey(), String.valueOf(entry.getValue()));
      }
    }
    LOG.info("Resources to load {}", resourcesToLoad);
    // add configs from resources like hdfs-site.xml
    for (String pathStr : resourcesToLoad) {
      configuration.addResource(new Path(Paths.get(pathStr).toUri()));
    }
  }
  LOG.info("Initializing UGI with config {}", configuration);
  UserGroupInformation.setConfiguration(configuration);
}

代码示例来源:origin: apache/nifi

private static ClassLoader createBootstrapClassLoader() {
  //Get list of files in bootstrap folder
  final List<URL> urls = new ArrayList<>();
  try {
    Files.list(Paths.get("lib/bootstrap")).forEach(p -> {
      try {
        urls.add(p.toUri().toURL());
      } catch (final MalformedURLException mef) {
        LOGGER.warn("Unable to load " + p.getFileName() + " due to " + mef, mef);
      }
    });
  } catch (IOException ioe) {
    LOGGER.warn("Unable to access lib/bootstrap to create bootstrap classloader", ioe);
  }
  //Create the bootstrap classloader
  return new URLClassLoader(urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: wildfly/wildfly

@Override
public URL getUrl() {
  try {
    return file.toUri().toURL();
  } catch (MalformedURLException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: networknt/light-4j

/**
 * Returns a zip file system
 * @param zipFilename to construct the file system from
 * @param create true if the zip file should be created
 * @return a zip file system
 * @throws IOException
 */
private static FileSystem createZipFileSystem(String zipFilename, boolean create) throws IOException {
  // convert the filename to a URI
  final Path path = Paths.get(zipFilename);
  if(Files.notExists(path.getParent())) {
    Files.createDirectories(path.getParent());
  }
  final URI uri = URI.create("jar:file:" + path.toUri().getPath());
  final Map<String, String> env = new HashMap<>();
  if (create) {
    env.put("create", "true");
  }
  return FileSystems.newFileSystem(uri, env);
}

代码示例来源:origin: twosigma/beakerx

public PathToJar(final String path) {
 checkNotNull(path);
 File file = getFile(path);
 try {
  canonicalPath = file.getCanonicalPath();
  this.url = Paths.get(canonicalPath).toUri().toURL();
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: allure-framework/allure2

private Optional<URL> toUrlSafe(final Path path) {
    try {
      return Optional.of(path.toUri().toURL());
    } catch (MalformedURLException e) {
      LOGGER.error("Could not load {}: {}", path, e);
      return Optional.empty();
    }
  }
}

相关文章