下面列出了java.nio.file.Path#toAbsolutePath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void validate(Path templatePath, Map<String, String> inputParams, Path output) {
if (!Files.exists(templatePath)) {
throw new IllegalArgumentException("Provided template path '" + templatePath +
"' does not exist.");
}
output = output.toAbsolutePath();
Path outputParent = output.getParent();
if (outputParent != null && !Files.exists(outputParent)) {
throw new IllegalArgumentException("Output cannot be created, its parent folder does not exist'" +
output.getParent());
} else if (Files.exists(output)) {
logger.warn("Output path exists already. It will be overwritten: " + output);
}
}
private static String getTool(String tool, String property) throws FileNotFoundException {
String jdkPath = System.getProperty(property);
if (jdkPath == null) {
throw new RuntimeException(
"System property '" + property + "' not set. This property is normally set by jtreg. "
+ "When running test separately, set this property using '-D" + property + "=/path/to/jdk'.");
}
Path toolName = Paths.get("bin", tool + (Platform.isWindows() ? ".exe" : ""));
Path jdkTool = Paths.get(jdkPath, toolName.toString());
if (!jdkTool.toFile().exists()) {
throw new FileNotFoundException("Could not find file " + jdkTool.toAbsolutePath());
}
return jdkTool.toAbsolutePath().toString();
}
/**
* Create a PathFileObject whose binary name might be inferred from its
* position on a search path.
*/
static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager,
final Path path) {
return new PathFileObject(fileManager, path) {
@Override
String inferBinaryName(Iterable<? extends Path> paths) {
Path absPath = path.toAbsolutePath();
for (Path p: paths) {
Path ap = p.toAbsolutePath();
if (absPath.startsWith(ap)) {
try {
Path rp = ap.relativize(absPath);
if (rp != null) // maybe null if absPath same as ap
return toBinaryName(rp);
} catch (IllegalArgumentException e) {
// ignore this p if cannot relativize path to p
}
}
}
return null;
}
};
}
public ResultsAggregator(final Path directory, final double reportOutputScalingRatio)
{
if (!exists(directory))
{
throw new IllegalArgumentException("directory does not exist: " + directory.toAbsolutePath());
}
if (!isDirectory(directory))
{
throw new IllegalArgumentException(directory.toAbsolutePath() + " is not a directory!");
}
if (isNaN(reportOutputScalingRatio) || compare(reportOutputScalingRatio, 0.0) <= 0)
{
throw new IllegalArgumentException(
"report output value scale ratio must a positive number, got: " + reportOutputScalingRatio);
}
this.directory = directory;
this.reportOutputScalingRatio = reportOutputScalingRatio;
}
void setupRootPath() {
if (root != null) {
throw new RuntimeException("Not allowed to setup mod root path twice!");
}
try {
Path holder = UrlUtil.asPath(originUrl).toAbsolutePath();
if (Files.isDirectory(holder)) {
root = holder.toAbsolutePath();
} else /* JAR */ {
FileSystemUtil.FileSystemDelegate delegate = FileSystemUtil.getJarFileSystem(holder, false);
if (delegate.get() == null) {
throw new RuntimeException("Could not open JAR file " + holder.getFileName() + " for NIO reading!");
}
root = delegate.get().getRootDirectories().iterator().next();
// We never close here. It's fine. getJarFileSystem() will handle it gracefully, and so should mods
}
} catch (IOException | UrlConversionException e) {
throw new RuntimeException("Failed to find root directory for mod '" + info.getId() + "'!", e);
}
}
private static String getTool(String tool, String property) throws FileNotFoundException {
String jdkPath = System.getProperty(property);
if (jdkPath == null) {
throw new RuntimeException(
"System property '" + property + "' not set. This property is normally set by jtreg. "
+ "When running test separately, set this property using '-D" + property + "=/path/to/jdk'.");
}
Path toolName = Paths.get("bin", tool + (Platform.isWindows() ? ".exe" : ""));
Path jdkTool = Paths.get(jdkPath, toolName.toString());
if (!jdkTool.toFile().exists()) {
throw new FileNotFoundException("Could not find file " + jdkTool.toAbsolutePath());
}
return jdkTool.toAbsolutePath().toString();
}
private static String getTool(String tool, String property) throws FileNotFoundException {
String jdkPath = System.getProperty(property);
if (jdkPath == null) {
throw new RuntimeException(
"System property '" + property + "' not set. This property is normally set by jtreg. "
+ "When running test separately, set this property using '-D" + property + "=/path/to/jdk'.");
}
Path toolName = Paths.get("bin", tool + (Platform.isWindows() ? ".exe" : ""));
Path jdkTool = Paths.get(jdkPath, toolName.toString());
if (!jdkTool.toFile().exists()) {
throw new FileNotFoundException("Could not find file " + jdkTool.toAbsolutePath());
}
return jdkTool.toAbsolutePath().toString();
}
@MCRCommand(help = "Imports a transfer package located at {0}. Where {0} is the absolute path to the tar file. "
+ "The parameter {1} is optional and can be omitted. You can specify a mycore id where the first object of "
+ "import.xml should be attached.",
syntax = "import transfer package from tar {0} to {1}")
public static List<String> importTransferPackageFromTar(String pathToTar, String mycoreTargetId) throws Exception {
Path tar = Paths.get(pathToTar);
if (!Files.exists(tar)) {
throw new FileNotFoundException(tar.toAbsolutePath() + " does not exist.");
}
Path targetDirectory = MCRTransferPackageUtil.getTargetDirectory(tar);
List<String> commands = new ArrayList<>();
commands.add("_import transfer package untar " + pathToTar);
commands.add("_import transfer package from directory " + targetDirectory + " to " + mycoreTargetId);
commands.add("_import transfer package clean up " + targetDirectory);
return commands;
}
/**
* Take the value from environment variable and assert that it is a path, and the file or
* directory, specified by this path, exists.
*
* @param name name of the environment variable
* @return Path to the file where the value of environment variable points
*/
private static Path getPathFromEnv(String name) {
String pathStr = System.getenv(name);
assertThat(pathStr).isNotNull();
Path path = Paths.get(pathStr);
assertThat(Files.exists(path)).isTrue();
return path.toAbsolutePath();
}
public static void main(String[] args) {
// Using Paths.get(...) to create a Path
Path dictionary = Paths.get("/", "usr", "share", "dict", "web2");
System.out.println(dictionary);
Path home = Paths.get("/Users/kousen");
System.out.println(home);
// Using resolve to find nested paths
Path docs = home.resolve("Documents");
System.out.println(docs);
// Can resolve siblings as well
Path downloads = docs.resolveSibling("Downloads");
System.out.println(downloads);
// Project directory
Path project = Paths.get(".");
System.out.println(project);
System.out.println(project.toAbsolutePath());
System.out.println("As a URI: " + project.toUri());
// Normalize a path
Path p = Paths.get("/Users/kousen/Documents/./IntelliJ/..").normalize();
System.out.println("Normalized: " + p);
System.out.println("parent: " + project.toAbsolutePath().getParent());
System.out.println("file name: " + project.toAbsolutePath().getFileName());
System.out.println("root: " + project.toAbsolutePath().getRoot());
for (Path path : project.toAbsolutePath()) {
System.out.println(path);
}
File localDir = new File("..");
System.out.println(localDir);
System.out.println(localDir.toPath().toAbsolutePath().normalize());
}
public LiveSource(Path filePath, long timeout, TimeUnit timeUnit) {
this.timeout = timeout;
this.timeUnit = timeUnit;
this.filePath = filePath.toAbsolutePath();
resetLastTick();
handleFileChange();
final Thread watcherThread = new Thread(this::watcherThread);
watcherThread.setName("clarity-livesource-watcher");
watcherThread.setDaemon(true);
watcherThread.start();
}
/**
* Returns the path of the given source file relative to the common input source directory.
*
* @throws IllegalArgumentException if the given file is not under the common source directory.
*/
public Path getSourceRelativePath(Path sourceFile) {
if (sourcePrefix.isAbsolute()) {
sourceFile = sourceFile.toAbsolutePath();
}
checkArgument(
sourceFile.startsWith(sourcePrefix),
"The requested path is not a recognized source file: %s",
sourceFile);
return sourcePrefix.relativize(sourceFile.normalize());
}
/**
* Deletes a given directory.
*
* @param path path to directory
* @throws KubernetesPluginException if an error occurs while deleting
*/
public static void deleteDirectory(Path path) throws KubernetesPluginException {
Path pathToBeDeleted = path.toAbsolutePath();
if (!Files.exists(pathToBeDeleted)) {
return;
}
try {
Files.walk(pathToBeDeleted)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
throw new KubernetesPluginException("unable to delete directory: " + path, e);
}
}
private byte[] getFileBytes(String dataFile) {
Path dataPath = Paths.get(dataFile);
try {
return Files.readAllBytes(dataPath);
} catch (Exception e) {
throw new RuntimeException("While getting data from " + dataPath.toAbsolutePath(), e);
}
}
private List<String> readPasswords(final Path passwords) throws IOException {
try {
return Files.readAllLines(passwords);
} catch (final IOException ex) {
throw new IOException("Failed to read password list '" + passwords.toAbsolutePath() + "'", ex);
}
}
private Path getStates( final Path projectRoot, final String filePath ) {
if ( projectRoot != null && filePath != null ) {
logger.debug( "Looking for Golden Master files in '{}'.", projectRoot );
final Path statePath = Paths.get( projectRoot.toAbsolutePath().toString(), filePath );
if ( statePath.toFile().exists() ) {
return statePath.toAbsolutePath();
}
}
return null;
}
public BottomUpPathMerger(
Iterable<IjFolder> foldersToWalk,
int limit,
Path moduleLocation,
PackagePathCache packagePathCache,
ImmutableSet<Path> traversalBoundaryPaths) {
this.tree = new MutableDirectedGraph<>();
this.packagePathCache = packagePathCache;
this.mergePathsMap = new HashMap<>();
this.moduleLocation = moduleLocation.toAbsolutePath();
for (IjFolder folder : foldersToWalk) {
Path path = folder.getPath();
mergePathsMap.put(path, folder);
while (getPathNameCount(path) > limit) {
Path parent = this.moduleLocation.resolve(path).getParent();
if (parent == null) {
break;
}
parent = this.moduleLocation.relativize(parent);
boolean isParentAndGrandParentAlreadyInTree = tree.containsNode(parent);
tree.addEdge(parent, path);
if (isParentAndGrandParentAlreadyInTree) {
break;
}
path = parent;
}
}
topLevels = findTopLevels(foldersToWalk, traversalBoundaryPaths);
}
@Override
public String getDeleteCommand (Path file)
{
return "rm -v -f \"" + file.toAbsolutePath() + "\"";
}
static String deleteFile(Path p) {
return "Failed to delete " + p.toAbsolutePath();
}
Kontalk(Path appDir) {
mAppDir = appDir.toAbsolutePath();
}