本文整理汇总了Java中java.nio.file.Path.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java Path.getParent方法的具体用法?Java Path.getParent怎么用?Java Path.getParent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Path
的用法示例。
在下文中一共展示了Path.getParent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* @param args source and destination
* @throws IOException if an I/O error occurs
*/
public static void main(String[] args) throws IOException {
if (args.length != 2) {
throw new IllegalArgumentException("Unexpected number of arguments for file copy");
}
Path src = Paths.get(Utils.TEST_SRC, args[0]).toAbsolutePath();
Path dst = Paths.get(args[1]).toAbsolutePath();
if (src.toFile().exists()) {
if (src.toFile().isDirectory()) {
Files.walkFileTree(src, new CopyFileVisitor(src, dst));
} else {
Path dstDir = dst.getParent();
if (!dstDir.toFile().exists()) {
Files.createDirectories(dstDir);
}
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
}
} else {
throw new IOException("Can't find source " + src);
}
}
示例2: baseURL
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Returns the base directory or URL for the given URL. Used to implement __DIR__.
* @param url a URL
* @return base path or URL, or null if argument is not a hierarchical URL
*/
public static String baseURL(final URL url) {
try {
final URI uri = url.toURI();
if (uri.getScheme().equals("file")) {
final Path path = Paths.get(uri);
final Path parent = path.getParent();
return (parent != null) ? (parent + File.separator) : null;
}
if (uri.isOpaque() || uri.getPath() == null || uri.getPath().isEmpty()) {
return null;
}
return uri.resolve("").toString();
} catch (final SecurityException | URISyntaxException | IOError e) {
return null;
}
}
示例3: dumpClass
import java.nio.file.Path; //导入方法依赖的package包/类
public void dumpClass(String className, final byte[] classBytes) {
Path file;
try {
file = dumpDir.resolve(encodeForFilename(className) + ".class");
} catch (InvalidPathException ex) {
PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
.warning("Invalid path for class " + className);
return;
}
try {
Path dir = file.getParent();
Files.createDirectories(dir);
Files.write(file, classBytes);
} catch (Exception ignore) {
PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
.warning("Exception writing to path at " + file.toString());
// simply don't care if this operation failed
}
}
示例4: getFileToCreate
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Gets file to create.
*
* @return the file to creating.
*/
@FromAnyThread
protected @Nullable Path getFileToCreate() {
final TextField fileNameField = getFileNameField();
final String filename = fileNameField.getText();
if (StringUtils.isEmpty(filename)) return null;
final String fileExtension = getFileExtension();
final Path selectedFile = getSelectedFile();
final Path directory = Files.isDirectory(selectedFile) ? selectedFile : selectedFile.getParent();
return StringUtils.isEmpty(fileExtension) ? directory.resolve(filename) :
directory.resolve(filename + "." + fileExtension);
}
示例5: createRecordingFilePath
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Generates the path to a recording file based on when a recording started. The generated path is in the format
* {@code /Shuffleboard/recordings/<date>/recording-<time>.sbr}, where {@code date} is the date formatted by the
* ISO-8601 format, and {@code time} is a modified version that uses periods ({@code "."}) instead of colons because
* Windows does not allow colon characters in file names.
*
* @param startTime the time the recording started
*/
public static Path createRecordingFilePath(Instant startTime) throws IOException {
String date = dateFormatter.format(LocalDateTime.ofInstant(startTime, ZoneId.systemDefault()));
String time = timeFormatter.format(LocalDateTime.ofInstant(startTime, ZoneId.systemDefault()));
Path file = Paths.get(RECORDING_FILE_FORMAT
.replace("${date}", date)
.replace("${time}", time));
Path parent = file.getParent();
if (parent != null && !Files.exists(parent)) {
Files.createDirectories(parent);
}
return file;
}
示例6: test0
import java.nio.file.Path; //导入方法依赖的package包/类
static void test0(FileSystem fs)
throws Exception
{
List<String> list = new LinkedList<>();
try (ZipFile zf = new ZipFile(fs.toString())) {
Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) {
list.add(zes.nextElement().getName());
}
for (String pname : list) {
Path path = fs.getPath(pname);
if (!Files.exists(path))
throw new RuntimeException("path existence check failed!");
while ((path = path.getParent()) != null) {
if (!Files.exists(path))
throw new RuntimeException("parent existence check failed!");
}
}
}
}
示例7: copyFile
import java.nio.file.Path; //导入方法依赖的package包/类
private static void copyFile(File f) {
try {
Path classFilePath = Paths.get(classFile);
String packagePath = classFilePath.getParent() == null ? "" : classFilePath.getParent().toString();
Path p = Paths.get(destinationDir + packagePath + File.separator + f.getName());
Files.createDirectories(p.getParent());
try (InputStream is = new FileInputStream(f)) {
Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
}
if (removeSource && !f.delete()) {
throw new RuntimeException("Failed to delete a file");
}
} catch (IOException ex) {
throw new RuntimeException("Could not copy file " + f, ex);
}
}
示例8: getCoreFileLocation
import java.nio.file.Path; //导入方法依赖的package包/类
private String getCoreFileLocation(String crashOutputString) {
Asserts.assertTrue(crashOutputString.contains(LOCATIONS_STRING),
"Output doesn't contain the location of core file, see crash.out");
String stringWithLocation = Arrays.stream(crashOutputString.split("\\r?\\n"))
.filter(str -> str.contains(LOCATIONS_STRING))
.findFirst()
.get();
stringWithLocation = stringWithLocation.substring(stringWithLocation
.indexOf(LOCATIONS_STRING) + LOCATIONS_STRING.length());
String coreWithPid;
if (stringWithLocation.contains("or ") && !Platform.isWindows()) {
Matcher m = Pattern.compile("or.* ([^ ]+[^\\)])\\)?").matcher(stringWithLocation);
if (!m.find()) {
throw new Error("Couldn't find path to core inside location string");
}
coreWithPid = m.group(1);
} else {
coreWithPid = stringWithLocation.trim();
}
if (new File(coreWithPid).exists()) {
return coreWithPid;
}
String justCore = Paths.get("core").toString();
if (new File(justCore).exists()) {
return justCore;
}
Path coreWithPidPath = Paths.get(coreWithPid);
String justFile = coreWithPidPath.getFileName().toString();
if (new File(justFile).exists()) {
return justFile;
}
Path parent = coreWithPidPath.getParent();
if (parent != null) {
String coreWithoutPid = parent.resolve("core").toString();
if (new File(coreWithoutPid).exists()) {
return coreWithoutPid;
}
}
return null;
}
示例9: createJarFile
import java.nio.file.Path; //导入方法依赖的package包/类
public static Path createJarFile(Path jarfile, Path dir, Path file) throws IOException {
// create the target directory
Path parent = jarfile.getParent();
if (parent != null)
Files.createDirectories(parent);
List<Path> entries = Files.find(dir.resolve(file), Integer.MAX_VALUE,
(p, attrs) -> attrs.isRegularFile())
.map(dir::relativize)
.collect(Collectors.toList());
try (OutputStream out = Files.newOutputStream(jarfile);
JarOutputStream jos = new JarOutputStream(out)) {
for (Path entry : entries) {
// map the file path to a name in the JAR file
Path normalized = entry.normalize();
String name = normalized
.subpath(0, normalized.getNameCount()) // drop root
.toString()
.replace(File.separatorChar, '/');
jos.putNextEntry(new JarEntry(name));
Files.copy(dir.resolve(entry), jos);
}
}
return jarfile;
}
示例10: getProjectForFile
import java.nio.file.Path; //导入方法依赖的package包/类
private Project getProjectForFile(Path filePath, Map<Path, Project> projectsByPath) {
Path currentDirectory = filePath.getParent();
while (!currentDirectory.toAbsolutePath().toString().isEmpty()) {
Project project = projectsByPath.get(currentDirectory);
if (project != null) {
return project;
}
currentDirectory = currentDirectory.getParent();
}
throw new IllegalStateException(
"Could not find project for a file in the project, this cannot happen: " + filePath);
}
示例11: writeFile
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Writes a file containing the given content.
* Any necessary directories for the file will be created.
* @param path where to write the file
* @param content the content for the file
* @throws IOException if an error occurred while writing the file
*/
public void writeFile(Path path, String content) throws IOException {
Path dir = path.getParent();
if (dir != null)
Files.createDirectories(dir);
try (BufferedWriter w = Files.newBufferedWriter(path)) {
w.write(content);
}
}
示例12: ProguardFileParser
import java.nio.file.Path; //导入方法依赖的package包/类
ProguardFileParser(Path path) throws IOException {
this.path = path;
contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
baseDirectory = path.getParent();
if (baseDirectory == null) {
// path parent can be null only if it's root dir or if its a one element path relative to
// current directory.
baseDirectory = Paths.get(".");
}
}
示例13: checkName
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* The checking of the new file name.
*/
private Boolean checkName(@NotNull final String newFileName) {
if (!FileUtils.isValidName(newFileName)) return false;
final Path file = getElement().getFile();
final String extension = FileUtils.getExtension(file);
final Path parent = file.getParent();
final Path targetFile = parent.resolve(StringUtils.isEmpty(extension) ? newFileName : newFileName + "." + extension);
return !Files.exists(targetFile);
}
示例14: createJar
import java.nio.file.Path; //导入方法依赖的package包/类
/**
* Create a jar file using the list of files provided.
*/
public static void createJar(Path jarfile, Path root, Stream<Path> files)
throws IOException {
Path dir = jarfile.getParent();
if (dir != null && Files.notExists(dir)) {
Files.createDirectories(dir);
}
try (JarOutputStream target = new JarOutputStream(
Files.newOutputStream(jarfile))) {
files.forEach(file -> add(root.relativize(file), file, target));
}
}
示例15: init
import java.nio.file.Path; //导入方法依赖的package包/类
private static List<Archive> init() {
List<Archive> result = new ArrayList<>();
Path home = Paths.get(System.getProperty("java.home"));
try {
if (home.endsWith("jre")) {
// jar files in <javahome>/jre/lib
result.addAll(addJarFiles(home.resolve("lib")));
if (home.getParent() != null) {
// add tools.jar and other JDK jar files
Path lib = home.getParent().resolve("lib");
if (Files.exists(lib)) {
result.addAll(addJarFiles(lib));
}
}
} else if (Files.exists(home.resolve("lib"))) {
// either a JRE or a jdk build image
Path classes = home.resolve("classes");
if (Files.isDirectory(classes)) {
// jdk build outputdir
result.add(new JDKArchive(classes));
}
// add other JAR files
result.addAll(addJarFiles(home.resolve("lib")));
} else {
throw new RuntimeException("\"" + home + "\" not a JDK home");
}
return result;
} catch (IOException e) {
throw new Error(e);
}
}