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

x33g5p2x  于2022-01-18 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(379)

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

FileSystems.newFileSystem介绍

暂无

代码示例

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

private static FileSystem getFileSystem(URI uri) throws IOException {
  try {
    return FileSystems.getFileSystem(uri);
  } catch (FileSystemNotFoundException e) {
    return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap());
  }
}

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

private void initFileSystem(URI uri) throws IOException {
  try {
    FileSystems.getFileSystem(uri);
  }
  catch (FileSystemNotFoundException e) {
    FileSystems.newFileSystem(uri, Collections.emptyMap());
  }
  catch (IllegalArgumentException e) {
    FileSystems.getDefault();
  }
}

代码示例来源:origin: CalebFenton/simplify

private static void updateZip(File zip, File entry, String entryName) throws IOException {
  Map<String, String> env = new HashMap<>();
  String uriPath = "jar:" + zip.toURI().toString();
  URI uri = URI.create(uriPath);
  try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
    fs.provider().checkAccess(fs.getPath(entryName), AccessMode.READ);
    Path target = fs.getPath(entryName);
    Path source = entry.toPath();
    Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
  } catch (IOException e) {
    throw e;
  }
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public FileSystem load(@Nonnull URI key) throws Exception {
    try {
      return FileSystems.getFileSystem(key);
    } catch (FileSystemNotFoundException e) {
      try {
        return FileSystems.newFileSystem(key, Collections.emptyMap());
      } catch (FileSystemAlreadyExistsException f) {
        return FileSystems.getFileSystem(key);
      }
    }
  }
});

代码示例来源: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: square/wire

private ImmutableList<ProfileFileElement> loadProfileFiles(Multimap<Path, String> pathsToAttempt)
  throws IOException {
 ImmutableList.Builder<ProfileFileElement> result = ImmutableList.builder();
 try (Closer closer = Closer.create()) {
  for (Map.Entry<Path, Collection<String>> entry : pathsToAttempt.asMap().entrySet()) {
   Path base = entry.getKey();
   if (Files.isRegularFile(base)) {
    FileSystem sourceFs = FileSystems.newFileSystem(base, getClass().getClassLoader());
    closer.register(sourceFs);
    base = getOnlyElement(sourceFs.getRootDirectories());
   }
   for (String path : entry.getValue()) {
    ProfileFileElement element = loadProfileFile(base, path);
    if (element != null) result.add(element);
   }
  }
 }
 return result.build();
}

代码示例来源:origin: square/wire

public Schema load() throws IOException {
 if (sources.isEmpty()) {
  throw new IllegalStateException("No sources added.");
 }
 try (Closer closer = Closer.create()) {
  // Map the physical path to the file system root. For regular directories the key and the
  // value are equal. For ZIP files the key is the path to the .zip, and the value is the root
  // of the file system within it.
  Map<Path, Path> directories = new LinkedHashMap<>();
  for (Path source : sources) {
   if (Files.isRegularFile(source)) {
    FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader());
    closer.register(sourceFs);
    directories.put(source, getOnlyElement(sourceFs.getRootDirectories()));
   } else {
    directories.put(source, source);
   }
  }
  return loadFromDirectories(directories);
 }
}

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

/** Returns a reference-counted Jar FileSystem, possibly one that was previously returned. */
private static FileSystem getJarFs(Path jarFile) throws IOException {
 Path key = jarFile.toAbsolutePath();
 synchronized (ZIP_FILESYSTEMS) {
  FsWrapper fs = ZIP_FILESYSTEMS.get(key);
  if (fs == null) {
   fs = new FsWrapper(FileSystems.newFileSystem(key, null), key);
   fs.incrRefCount();
   ZIP_FILESYSTEMS.put(key, fs);
  } else {
   fs.incrRefCount();
  }
  return fs;
 }
}

代码示例来源:origin: google/jimfs

@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException {
 JimfsPath checkedPath = checkPath(path);
 checkNotNull(env);
 URI pathUri = checkedPath.toUri();
 URI jarUri = URI.create("jar:" + pathUri);
 try {
  // pass the new jar:jimfs://... URI to be handled by ZipFileSystemProvider
  return FileSystems.newFileSystem(jarUri, env);
 } catch (Exception e) {
  // if any exception occurred, assume the file wasn't a zip file and that we don't support
  // viewing it as a file system
  throw new UnsupportedOperationException(e);
 }
}

代码示例来源:origin: MovingBlocks/Terasology

protected byte[] loadChunkZip(Vector3i chunkPos) {
  byte[] chunkData = null;
  Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos);
  Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos);
  if (Files.isRegularFile(chunkPath)) {
    try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) {
      Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos));
      if (Files.isRegularFile(targetChunk)) {
        chunkData = Files.readAllBytes(targetChunk);
      }
    } catch (IOException e) {
      logger.error("Failed to load chunk zip {}", chunkPath, e);
    }
  }
  return chunkData;
}

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

URI archiveAbsoluteURI = URI.create( "jar:file:" + destinationZip.toURI().getRawPath() );
try ( FileSystem zipFs = FileSystems.newFileSystem( archiveAbsoluteURI, env ) )

代码示例来源:origin: confluentinc/ksql

@SuppressWarnings("ConstantConditions")
 public static Path getResourceRoot() throws IOException, URISyntaxException {
  final URI uri = GeneratorTest.class.getClassLoader().getResource("product.avro").toURI();
  if ("jar".equals(uri.getScheme())) {
   final FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap(), null);
   return fileSystem.getPath("path/to/folder/inside/jar").getParent();
  } else {
   return Paths.get(uri).getParent();
  }
 }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

static void createJar(final URI jarURI, final File workDir, final File f) throws Exception {
  final Map<String, String> env = new HashMap<>(); 
  env.put("create", "true");
  final URI uri = URI.create("jar:file://" + jarURI.getRawPath());
  try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {   
    final Path path = zipfs.getPath(workDir.toPath().relativize(f.toPath()).toString());
    if (path.getParent() != null) {
      Files.createDirectories(path.getParent());
    }
    Files.copy(f.toPath(),
        path, 
        StandardCopyOption.REPLACE_EXISTING ); 
  } 
}

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

private void addJarManifest(String uberJar, MavenProject project, String mainClass) throws IOException {
  Path path = Paths.get(uberJar);
  URI uri = URI.create("jar:" + path.toUri());
  String user = System.getProperty("user.name");
  String manifestContent = IO.load("manifest-template.mf")
    .replace("$user", user)
    .replace("$java", Msc.javaVersion())
    .replace("$name", project.getName())
    .replace("$version", project.getVersion())
    .replace("$groupId", project.getGroupId())
    .replace("$organization", project.getOrganization() != null ? U.or(project.getOrganization().getName(), "?") : "?")
    .replace("$url", U.or(project.getUrl(), "?"))
    .replace("$main", U.safe(mainClass));
  try (FileSystem fs = FileSystems.newFileSystem(uri, U.map())) {
    Path manifest = fs.getPath("META-INF/MANIFEST.MF");
    try (Writer writer = Files.newBufferedWriter(manifest, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
      writer.write(manifestContent);
    }
  }
}

代码示例来源:origin: apache/incubator-gobblin

@BeforeClass
public void setUp() throws URISyntaxException, ConfigStoreCreationException, IOException {
 Path path = Paths.get(this.getClass().getClassLoader().getResource("zipStoreTest.zip").getPath());
 FileSystem fs = FileSystems.newFileSystem(path, null);
 this.store = new ZipFileConfigStore((ZipFileSystem) fs, path.toUri(), this.version, "_CONFIG_STORE");
}

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

private static void verifyContent( Path destination ) throws IOException
{
  URI uri = URI.create("jar:file:" + destination.toAbsolutePath().toUri().getRawPath() );
  try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) )
  {
    List<String> fileA = Files.readAllLines( fs.getPath( "logs/a.txt" ) );
    assertEquals( 1, fileA.size() );
    assertEquals( "file a", fileA.get( 0 ) );
    List<String> fileB = Files.readAllLines( fs.getPath( "logs/b.txt" ) );
    assertEquals( 1, fileB.size() );
    assertEquals( "file b", fileB.get( 0 ) );
  }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testNewFileSystem() throws Exception {
 Map<String, String> env = new HashMap<>();
 FileSystems.newFileSystem(URI.create("gs://bucket/path/to/file"), env);
}

代码示例来源:origin: jooby-project/jooby

private Library loadLibrary(final URI lib) {
 return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap()))
   .map(it -> Try.apply(() -> new Library(it)).get())
   .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get())
   .get();
}

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

try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) )

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

final URI uri = URI.create( "jar:file:" + report.toUri().getRawPath() );
try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) )

相关文章