本文整理汇总了Java中java.nio.file.FileSystems.newFileSystem方法的典型用法代码示例。如果您正苦于以下问题:Java FileSystems.newFileSystem方法的具体用法?Java FileSystems.newFileSystem怎么用?Java FileSystems.newFileSystem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.FileSystems
的用法示例。
在下文中一共展示了FileSystems.newFileSystem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onTest
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Test
public void onTest() throws Exception {
File testZip = new File("test.zip");
JarFile jarFile = new JarFile(testZip);
jarFile.stream().forEach((jarEntry -> {
System.out.println(jarEntry.getName());
if (jarEntry.getName().lastIndexOf('/') != -1) {
System.out.println( "parent -> " + jarEntry.getName().substring(0, jarEntry
.getName()
.lastIndexOf('/')));
}
}));
System.out.println("Filesystems");
FileSystem fileSystem = FileSystems.newFileSystem(testZip.toPath(), null);
Path meow = fileSystem.getPath("meow/");
Files.list(meow).forEach((path) -> System.out.println(path.getFileName()));
}
示例2: ArchiveContainer
import java.nio.file.FileSystems; //导入方法依赖的package包/类
public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundException, SecurityException {
this.archivePath = archivePath;
if (multiReleaseValue != null && archivePath.toString().endsWith(".jar")) {
Map<String,String> env = Collections.singletonMap("multi-release", multiReleaseValue);
FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider();
Assert.checkNonNull(jarFSProvider, "should have been caught before!");
this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
} else {
this.fileSystem = FileSystems.newFileSystem(archivePath, null);
}
packages = new HashMap<>();
for (Path root : fileSystem.getRootDirectories()) {
Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (isValid(dir.getFileName())) {
packages.put(new RelativeDirectory(root.relativize(dir).toString()), dir);
return FileVisitResult.CONTINUE;
} else {
return FileVisitResult.SKIP_SUBTREE;
}
}
});
}
}
示例3: validate
import java.nio.file.FileSystems; //导入方法依赖的package包/类
static void validate(Module module) throws IOException {
ModuleDescriptor md = module.getDescriptor();
// read m1/module-info.class
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
Collections.emptyMap());
Path path = fs.getPath("/", "modules", module.getName(), "module-info.class");
ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path));
// check the module descriptor of a system module and read from jimage
checkPackages(md.packages(), "p1", "p2");
checkPackages(md1.packages(), "p1", "p2");
try (InputStream in = Files.newInputStream(path)) {
checkModuleTargetAttribute(in, "p1");
}
}
示例4: main
import java.nio.file.FileSystems; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
// load another package
p2.T.test();
// validate the module descriptor
validate(Main.class.getModule());
// validate the Moduletarget attribute for java.base
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
Collections.emptyMap());
Path path = fs.getPath("/", "modules", "java.base", "module-info.class");
try (InputStream in = Files.newInputStream(path)) {
if (! hasModuleTarget(in)) {
throw new RuntimeException("Missing ModuleTarget for java.base");
}
}
}
示例5: testNewFileSystemWithJavaHome
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Test
public void testNewFileSystemWithJavaHome() throws Exception {
if (isExplodedBuild) {
System.out.println("Skip testNewFileSystemWithJavaHome"
+ " since this is an exploded build");
return;
}
Map<String, String> env = new HashMap<>();
// set java.home property to be underlying java.home
// so that jrt-fs.jar loading is exercised.
env.put("java.home", System.getProperty("java.home"));
try (FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), env)) {
checkFileSystem(fs);
// jrt-fs.jar classes are loaded by another (non-boot) loader in this case
assertNotNull(fs.provider().getClass().getClassLoader());
}
}
示例6: testZipRepoContent
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Test
public void testZipRepoContent() throws Exception {
Path repoDir = Paths.get(getClass().getResource(repository).toURI());
Path mtarZip = null;
try {
mtarZip = step.zipRepoContent(repoDir.toAbsolutePath());
URI jarMtarUri = URI.create("jar:" + mtarZip.toAbsolutePath().toUri().toString());
try (FileSystem mtarFS = FileSystems.newFileSystem(jarMtarUri, new HashMap<>())) {
Path mtarRoot = mtarFS.getRootDirectories().iterator().next();
assertFalse(Files.exists(mtarRoot.resolve(".git")));
assertFalse(Files.exists(mtarRoot.resolve(".gitignore")));
assertTrue(Files.exists(mtarRoot.resolve("a/cool-script.script")));
assertTrue(Files.exists(mtarRoot.resolve("META-INF/mtad.yaml")));
assertTrue(Files.exists(mtarRoot.resolve("META-INF/MANIFEST.MF")));
}
} finally {
if (mtarZip != null) {
Files.deleteIfExists(mtarZip);
}
}
}
示例7: checkModule
import java.nio.file.FileSystems; //导入方法依赖的package包/类
private static void checkModule(String mn, String... packages) throws IOException {
// verify ModuleDescriptor from the runtime module
ModuleDescriptor md = ModuleLayer.boot().findModule(mn).get()
.getDescriptor();
checkModuleDescriptor(md, packages);
// verify ModuleDescriptor from module-info.class read from ModuleReader
try (InputStream in = ModuleFinder.ofSystem().find(mn).get()
.open().open("module-info.class").get()) {
checkModuleDescriptor(ModuleDescriptor.read(in), packages);
}
// verify ModuleDescriptor from module-info.class read from jimage
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of());
Path path = fs.getPath("/", "modules", mn, "module-info.class");
checkModuleDescriptor(ModuleDescriptor.read(Files.newInputStream(path)), packages);
}
示例8: readFromPath
import java.nio.file.FileSystems; //导入方法依赖的package包/类
/** Returns the application info read from either an app folder or ipa archive */
public static IosAppInfo readFromPath(Path ipaOrAppPath) throws IOException {
NSObject plistDict;
if (Files.isDirectory(ipaOrAppPath)) {
plistDict = PlistParser.fromPath(ipaOrAppPath.resolve("Info.plist"));
} else {
try (FileSystem ipaFs = FileSystems.newFileSystem(ipaOrAppPath, null)) {
Path appPath =
MoreFiles.listFiles(ipaFs.getPath("Payload"))
.stream()
// Can't use Files.isDirectory, because no entry is a "directory" in a zip.
.filter(e -> e.toString().endsWith(".app/"))
.collect(MoreCollectors.onlyElement());
plistDict = PlistParser.fromPath(appPath.resolve("Info.plist"));
}
}
return readFromPlistDictionary((NSDictionary) plistDict);
}
示例9: deserialize
import java.nio.file.FileSystems; //导入方法依赖的package包/类
private static synchronized Level deserialize(Path levelZip) throws IOException {
FileSystem fileSystem = FileSystems.newFileSystem(levelZip, null);
levelFile = fileSystem;
Path jsonPath = fileSystem.getPath("level.json");
EventBus eventBus = SpaceGame.getInstance().getEventBus();
Object ler = SpaceGame.getInstance().getContext().createLevelEventHandler(fileSystem);
eventBus.registerAnnotated(ler);
SpaceGame.getInstance().getContext().reload();
Level level = SpaceGame.getInstance().getGson().fromJson(Files.newBufferedReader(jsonPath), Level.class);
_deserializing = null;
levelFile = null;
level.init(ler);
return level;
}
示例10: toPath
import java.nio.file.FileSystems; //导入方法依赖的package包/类
/**
* Convert a local URL (file:// or jar:// protocol) to a {@link Path}
* @param resource the URL resource
* @return the Path
* @throws URISyntaxException
* @throws IOException
*/
public static Path toPath(URL resource) throws IOException, URISyntaxException {
if (resource == null) return null;
final String protocol = resource.getProtocol();
if ("file".equals(protocol)) {
return Paths.get(resource.toURI());
} else if ("jar".equals(protocol)) {
final String s = resource.toString();
final int separator = s.indexOf("!/");
final String entryName = s.substring(separator + 2);
final URI fileURI = URI.create(s.substring(0, separator));
final FileSystem fileSystem;
if (!jarFileSystems.contains(fileURI))
synchronized (jarFileSystems) {
if (jarFileSystems.add(fileURI)) {
fileSystem = FileSystems.newFileSystem(fileURI, Collections.<String, Object>emptyMap());
} else {
fileSystem = FileSystems.getFileSystem(fileURI);
}
} else {
fileSystem = FileSystems.getFileSystem(fileURI);
}
return fileSystem.getPath(entryName);
} else {
throw new IllegalArgumentException("Can't read " + resource + ", unknown protocol '" + protocol + "'");
}
}
示例11: main
import java.nio.file.FileSystems; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
int count = args.length > 0 ? Integer.valueOf(args[0]) : 0;
if (count < 1 || args.length != count*2+1) {
throw new IllegalArgumentException(Arrays.toString(args));
}
List<String> modules = List.of(Arrays.copyOfRange(args, 1, 1+count));
List<String> versions = List.of(Arrays.copyOfRange(args, 1+count, args.length));
for (int i=0; i < modules.size(); i++) {
System.out.format("module %s expects %s version%n",
modules.get(i), versions.get(i));
nameToVersion.put(modules.get(i), versions.get(i));
}
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
Collections.emptyMap());
// check the module descriptor of a system module
for (int i=0; i < modules.size(); i++) {
String mn = modules.get(i);
Module module = ModuleLayer.boot().findModule(mn).orElseThrow(
() -> new RuntimeException(mn + " not found")
);
// check ModuleDescriptor from the run-time
validate(module.getDescriptor());
// check module-info.class in the image
Path path = fs.getPath("/", "modules", modules.get(i), "module-info.class");
validate(ModuleDescriptor.read(Files.newInputStream(path)));
}
}
示例12: testJarFSPath
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Test
public void testJarFSPath(Path base) throws IOException {
// Test that we can look up in non-default file systems on the search path,
// such as an open jar file system.
Path src = base.resolve("src");
tb.writeJavaFiles(src, "package p; class C { }");
Path classes = Files.createDirectories(base.resolve("classes"));
new JavacTask(tb)
.options("-sourcepath", src.toString())
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
Path jar = base.resolve("classes.jar");
new JarTask(tb).run("cf", jar.toString(), "-C", classes.toString(), "p");
Path c = src.resolve("p/C.java");
Path x = base.resolve("src2/p/C.java");
try (FileSystem jarFS = FileSystems.newFileSystem(jar, null);
StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) {
Path jarRoot = jarFS.getRootDirectories().iterator().next();
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(src, jarRoot));
checkContains(fm, StandardLocation.CLASS_PATH, c, true);
checkContains(fm, StandardLocation.CLASS_PATH, x, false);
JavaFileObject fo = fm.list(StandardLocation.CLASS_PATH, "p",
EnumSet.of(JavaFileObject.Kind.CLASS), false).iterator().next();
checkContains(fm, StandardLocation.CLASS_PATH, fo, true);
checkContains(fm, StandardLocation.CLASS_PATH, jarRoot.resolve("p/C.class"), true);
}
}
示例13: getClassNames
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Override
public List<String> getClassNames() throws IOException {
Set<String> negative = new HashSet<>();
Set<String> positive = new HashSet<>();
if (limitModules != null && !limitModules.isEmpty()) {
for (String s : limitModules.split(",")) {
if (s.startsWith("~")) {
negative.add(s.substring(1));
} else {
positive.add(s);
}
}
}
List<String> classNames = new ArrayList<>();
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap());
Path top = fs.getPath("/modules/");
Files.find(top, Integer.MAX_VALUE,
(path, attrs) -> attrs.isRegularFile()).forEach(p -> {
int nameCount = p.getNameCount();
if (nameCount > 2) {
String base = p.getName(nameCount - 1).toString();
if (base.endsWith(".class") && !base.equals("module-info.class")) {
String module = p.getName(1).toString();
if (positive.isEmpty() || positive.contains(module)) {
if (negative.isEmpty() || !negative.contains(module)) {
// Strip module prefix and convert to dotted form
String className = p.subpath(2, nameCount).toString().replace('/', '.');
// Strip ".class" suffix
className = className.replace('/', '.').substring(0, className.length() - ".class".length());
classNames.add(className);
}
}
}
}
});
return classNames;
}
示例14: listModularRuntime
import java.nio.file.FileSystems; //导入方法依赖的package包/类
private Set<String> listModularRuntime(Path javaHome) throws IOException {
Map<String, String> env = new HashMap<>();
env.put("java.home", javaHome.toString());
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), env);
Path root = fs.getPath("packages");
return Files.walk(root, 1)
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toSet());
}
示例15: copyResources
import java.nio.file.FileSystems; //导入方法依赖的package包/类
@Override
public void copyResources(File dir) throws URISyntaxException, IOException {
if (!hasResources())
throw new UnsupportedOperationException(
"this plugin doesn't have any resources you can copy as you would know had you called hasResources first :P");
try (FileSystem zipFs =
FileSystems.newFileSystem(artifactURL.toURI(), new HashMap<>());) {
Path target = Paths.get(dir.toURI());
Path pathInZip = zipFs.getPath("/resources");
Files.walkFileTree(pathInZip, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path filePath,
BasicFileAttributes attrs) throws IOException {
// Make sure that we conserve the hierachy of files and folders
// inside the zip
Path relativePathInZip = pathInZip.relativize(filePath);
Path targetPath = target.resolve(relativePathInZip.toString());
Files.createDirectories(targetPath.getParent());
// And extract the file
Files.copy(filePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}