本文整理汇总了Java中java.nio.file.Files.isRegularFile方法的典型用法代码示例。如果您正苦于以下问题:Java Files.isRegularFile方法的具体用法?Java Files.isRegularFile怎么用?Java Files.isRegularFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.isRegularFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fileToString
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Reads a file and returns it as a string.
*/
public static String fileToString(final Path path, final Charset charset) {
final StringBuilder sb = new StringBuilder();
try {
if (Files.isRegularFile(path)) {
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
final char[] buffer = new char[4096];
int len;
while ((len = reader.read(buffer, 0, buffer.length)) != -1) {
sb.append(buffer, 0, len);
}
}
}
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
return sb.toString();
}
示例2: createFile
import java.nio.file.Files; //导入方法依赖的package包/类
public static void createFile(Path path, String... content) throws IOException {
if (Files.exists(path) && Files.isRegularFile(path)) {
try {
Files.delete(path);
} catch (Exception ex) {
System.out.println("WARNING: " + path.toFile().getAbsolutePath() + " already exists - unable to remove old copy");
ex.printStackTrace();
}
}
try (BufferedWriter bw = Files.newBufferedWriter(path, Charset.defaultCharset())) {
for (String str : content) {
bw.write(str, 0, str.length());
bw.newLine();
}
}
}
示例3: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage AddPackagesAttribute exploded-java-home");
System.exit(-1);
}
String home = args[0];
Path dir = Paths.get(home, "modules");
ModuleFinder finder = ModuleFinder.of(dir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
Path mi = entry.resolve("module-info.class");
if (Files.isRegularFile(mi)) {
String mn = entry.getFileName().toString();
Optional<ModuleReference> omref = finder.find(mn);
if (omref.isPresent()) {
Set<String> packages = omref.get().descriptor().packages();
addPackagesAttribute(mi, packages);
}
}
}
}
}
示例4: loadDatabaseReaders
import java.nio.file.Files; //导入方法依赖的package包/类
static Map<String, DatabaseReaderLazyLoader> loadDatabaseReaders(Path geoIpConfigDirectory, NodeCache cache) throws IOException {
if (Files.exists(geoIpConfigDirectory) == false && Files.isDirectory(geoIpConfigDirectory)) {
throw new IllegalStateException("the geoip directory [" + geoIpConfigDirectory + "] containing databases doesn't exist");
}
Map<String, DatabaseReaderLazyLoader> databaseReaders = new HashMap<>();
try (Stream<Path> databaseFiles = Files.list(geoIpConfigDirectory)) {
PathMatcher pathMatcher = geoIpConfigDirectory.getFileSystem().getPathMatcher("glob:**.mmdb.gz");
// Use iterator instead of forEach otherwise IOException needs to be caught twice...
Iterator<Path> iterator = databaseFiles.iterator();
while (iterator.hasNext()) {
Path databasePath = iterator.next();
if (Files.isRegularFile(databasePath) && pathMatcher.matches(databasePath)) {
String databaseFileName = databasePath.getFileName().toString();
DatabaseReaderLazyLoader holder = new DatabaseReaderLazyLoader(databaseFileName, () -> {
try (InputStream inputStream = new GZIPInputStream(Files.newInputStream(databasePath, StandardOpenOption.READ))) {
return new DatabaseReader.Builder(inputStream).withCache(cache).build();
}
});
databaseReaders.put(databaseFileName, holder);
}
}
}
return Collections.unmodifiableMap(databaseReaders);
}
示例5: assertRequiredArgs
import java.nio.file.Files; //导入方法依赖的package包/类
private void assertRequiredArgs(final Path nesstarMappingFile) {
if (nesstarMappingFile == null) {
throw new IllegalArgumentException(
"Required path to Clinical Indicators Compendium mapping file was not specified."
);
}
if (!Files.isRegularFile(nesstarMappingFile)) {
throw new IllegalArgumentException(
"Nesstar mapping file does not exist: " + nesstarMappingFile
);
}
}
示例6: test
import java.nio.file.Files; //导入方法依赖的package包/类
public void test() throws Exception {
// JPRT not yet ready for jmods
Helper helper = Helper.newHelper();
if (helper == null) {
System.err.println("Test not run, NO jmods directory");
return;
}
List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
Path moduleFile = helper.generateModuleCompiledClasses(
helper.getJmodSrcDir(), helper.getJmodClassesDir(), "leaf1", classes);
Path moduleInfo = moduleFile.resolve("module-info.class");
// Classes have been compiled in debug.
List<Path> covered = new ArrayList<>();
byte[] infoContent = Files.readAllBytes(moduleInfo);
try (Stream<Path> stream = Files.walk(moduleFile)) {
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) {
Path p = iterator.next();
if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
byte[] content = Files.readAllBytes(p);
String path = "/" + helper.getJmodClassesDir().relativize(p).toString();
String moduleInfoPath = path + "/module-info.class";
check(path, content, moduleInfoPath, infoContent);
covered.add(p);
}
}
}
if (covered.isEmpty()) {
throw new AssertionError("No class to compress");
} else {
System.err.println("removed debug attributes from "
+ covered.size() + " classes");
}
}
示例7: exists
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Returns whether the session with the given ID exists.
*/
public boolean exists(String sessionId) {
requireNonNull(sessionId, "sessionId");
final SimpleSession cachedSession = getFromCache(sessionId);
if (cachedSession != null) {
return true;
}
return Files.isRegularFile(sessionId2Path(sessionId));
}
示例8: getLauncher
import java.nio.file.Files; //导入方法依赖的package包/类
private static String[] getLauncher() throws IOException {
String platform = getPlatform();
if (platform == null) {
return null;
}
String launcher = TEST_SRC + File.separator + platform + "-" + ARCH +
File.separator + "launcher";
final FileSystem FS = FileSystems.getDefault();
Path launcherPath = FS.getPath(launcher);
final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&&
Files.isReadable(launcherPath);
if (!hasLauncher) {
System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test.");
return null;
}
// It is impossible to store an executable file in the source control
// We need to copy the launcher to the working directory
// and set the executable flag
Path localLauncherPath = FS.getPath(WORK_DIR, "launcher");
Files.copy(launcherPath, localLauncherPath,
StandardCopyOption.REPLACE_EXISTING);
if (!Files.isExecutable(localLauncherPath)) {
Set<PosixFilePermission> perms = new HashSet<>(
Files.getPosixFilePermissions(
localLauncherPath,
LinkOption.NOFOLLOW_LINKS
)
);
perms.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(localLauncherPath, perms);
}
return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()};
}
示例9: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
String home = System.getProperty("java.home");
Path modules = Paths.get(home, "lib", "modules");
if (!Files.isRegularFile(modules)) {
System.out.println("This runtime is not jimage, test skipped");
return;
}
boolean allow = args[0].equals("allow");
// set security policy to allow access
if (allow) {
String testSrc = System.getProperty("test.src");
if (testSrc == null)
testSrc = ".";
Path policyFile = Paths.get(testSrc, "java.policy");
System.setProperty("java.security.policy", policyFile.toString());
}
System.setSecurityManager(new SecurityManager());
InputStream in = null;
URL url = new URL("jrt:/java.base/java/lang/Object.class");
if (url != null) {
try {
in = url.openStream();
} catch (IOException ignore) { }
}
if (in == null && allow)
throw new RuntimeException("access expected");
if (in != null && !allow)
throw new RuntimeException("access not expected");
}
示例10: spawnNativePluginControllers
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* For each plugin, attempt to spawn the controller daemon. Silently ignore any plugins
* that don't include a controller for the correct platform.
*/
void spawnNativePluginControllers(Environment environment) throws IOException {
if (Files.exists(environment.pluginsFile())) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(environment.pluginsFile())) {
for (Path plugin : stream) {
Path spawnPath = makeSpawnPath(plugin);
if (Files.isRegularFile(spawnPath)) {
spawnNativePluginController(spawnPath, environment.tmpFile());
}
}
}
}
}
示例11: isRegularFile
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Returns a predicate that returns the result of
* {@link Files#isRegularFile(Path, LinkOption...)} on input paths with the given link options.
*/
public static Predicate<Path> isRegularFile(LinkOption... options) {
final LinkOption[] optionsCopy = options.clone();
return new Predicate<Path>() {
@Override
public boolean apply(Path input) {
return Files.isRegularFile(input, optionsCopy);
}
@Override
public String toString() {
return "MoreFiles.isRegularFile(" + Arrays.toString(optionsCopy) + ")";
}
};
}
示例12: corruptRandomPrimaryFile
import java.nio.file.Files; //导入方法依赖的package包/类
private ShardRouting corruptRandomPrimaryFile(final boolean includePerCommitFiles) throws IOException {
ClusterState state = client().admin().cluster().prepareState().get().getState();
Index test = state.metaData().index("test").getIndex();
GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[]{"test"}, false);
List<ShardIterator> iterators = iterableAsArrayList(shardIterators);
ShardIterator shardIterator = RandomPicks.randomFrom(random(), iterators);
ShardRouting shardRouting = shardIterator.nextOrNull();
assertNotNull(shardRouting);
assertTrue(shardRouting.primary());
assertTrue(shardRouting.assignedToNode());
String nodeId = shardRouting.currentNodeId();
NodesStatsResponse nodeStatses = client().admin().cluster().prepareNodesStats(nodeId).setFs(true).get();
Set<Path> files = new TreeSet<>(); // treeset makes sure iteration order is deterministic
for (FsInfo.Path info : nodeStatses.getNodes().get(0).getFs()) {
String path = info.getPath();
Path file = PathUtils.get(path).resolve("indices").resolve(test.getUUID()).resolve(Integer.toString(shardRouting.getId())).resolve("index");
if (Files.exists(file)) { // multi data path might only have one path in use
try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
for (Path item : stream) {
if (Files.isRegularFile(item) && "write.lock".equals(item.getFileName().toString()) == false) {
if (includePerCommitFiles || isPerSegmentFile(item.getFileName().toString())) {
files.add(item);
}
}
}
}
}
}
pruneOldDeleteGenerations(files);
CorruptionUtils.corruptFile(random(), files.toArray(new Path[0]));
return shardRouting;
}
示例13: loadHowToBook
import java.nio.file.Files; //导入方法依赖的package包/类
@Nullable ItemStack loadHowToBook(Path file) {
try {
if(!Files.isRegularFile(file)) return null;
return itemParser.parseBook(saxBuilder.build(file.toFile())
.getRootElement());
}
catch(JDOMException | IOException | InvalidXMLException e) {
logger.log(Level.SEVERE, "Failed to parse how-to book from XML file " + file, e);
return null;
}
}
示例14: listFiles
import java.nio.file.Files; //导入方法依赖的package包/类
private void listFiles(Path dir, Set<Path> files) throws IOException {
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {
for (Path f: ds) {
if (Files.isDirectory(f))
listFiles(f, files);
else if (Files.isRegularFile(f))
files.add(f);
}
}
}
示例15: isJarFile
import java.nio.file.Files; //导入方法依赖的package包/类
private static boolean isJarFile(Path path) {
if (Files.isRegularFile(path)) {
String name = path.toString();
return Utils.endsWithIgnoreCase(name, ".zip")
|| Utils.endsWithIgnoreCase(name, ".jar");
}
return false;
}