Java Utililty Methods Path File Name nio

List of utility methods to do Path File Name nio

Description

The list of methods to do Path File Name nio are organized into topic(s).

Method

voidaddPath(JarOutputStream outputStream, Path path, String entryName)
add Path
outputStream.putNextEntry(new JarEntry(entryName));
if (!Files.isDirectory(path)) {
    try (InputStream inputStream = Files.newInputStream(path)) {
        while (inputStream.available() > 0) {
            outputStream.write(inputStream.read());
outputStream.closeEntry();
voidaddRestorePermissions(String username, Path file)
Add the proper File-System permissions to a file so that SQL Server can run a RESTORE query.
AclFileAttributeView aclAttr = Files.getFileAttributeView(file, AclFileAttributeView.class);
UserPrincipalLookupService currULS = file.getFileSystem().getUserPrincipalLookupService();
UserPrincipal principal = currULS.lookupPrincipalByName(username);
AclEntry.Builder builder = AclEntry.newBuilder();
builder.setPermissions(EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ACL,
        AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.EXECUTE,
        AclEntryPermission.SYNCHRONIZE));
builder.setPrincipal(principal);
...
voidappendTargetToBuildFile(String targetName, Path dir)
append Target To Build File
appendLinesToFile(dir.resolve(BUILD_FILE_NAME), "cc_library(", "    name = '" + targetName + "',",
        "    srcs = [ '" + targetName + ".cc', '" + targetName + ".h' ],", ")");
voidcheckFile(final String friendlyname, final Path file, final boolean isdirectory, final boolean canwrite)
Check file access
if (file == null) {
    throw NULL_FILE_ARGUMENT_EXCEPTION;
final String exStartMsg = friendlyname + " = '" + file.toAbsolutePath() + "' ";
if (!Files.exists(file)) {
    throw new IllegalArgumentException(exStartMsg + "not found");
if (!Files.isReadable(file)) {
...
ClasscompileAndLoad(Path basePath, String className)
compile And Load
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Path file = basePath.resolve(className.replaceAll("\\.", File.separator) + JAVA);
int result = compiler.run(null, null, null, file.toString());
if (result != 0) {
    throw new IllegalStateException("Unable to compile generated code.");
URL[] urls = new URL[] { basePath.toUri().toURL() };
@SuppressWarnings("resource")
...
StringconcatFileName(String fileName, Path pathLocation)

concatenates a given filename to the provided path in directory.

final String windowsFolderSeparator = "\\";
final String unixFolderSeparator = "/";
StringBuilder path = new StringBuilder(pathLocation.toAbsolutePath().toString());
if (pathLocation.endsWith(windowsFolderSeparator)) {
    path = path.append(windowsFolderSeparator).append(fileName);
} else {
    path = path.append(unixFolderSeparator).append(fileName);
return path.toString();
StringconvertFilePathToName(Path file)
Turns a file path into a name suitable for use as the name of a premium or reserved list.
return Files.getNameWithoutExtension(file.getFileName().toString());
StringdetectFilePath(String propertyName, String confFileName)
detect File Path
String configFilePath = System.getProperty(propertyName);
if (configFilePath == null) {
    String currentDir = Paths.get(".").toAbsolutePath().normalize().toString();
    File confDir = new File(currentDir, CONF_DIR);
    if (confDir.exists() && confDir.isDirectory() && confDir.canRead()) {
        File dbConfigFile = new File(confDir, confFileName);
        if (dbConfigFile.exists()) {
            configFilePath = dbConfigFile.getAbsolutePath();
...
booleanendsWith(File file, String pathname)
ends With
Path path = file.toPath();
Path other = Paths.get(pathname);
int i = path.getNameCount() - 1;
int j = other.getNameCount() - 1;
for (; i >= 0 && j >= 0; i--, j--) {
    String element = path.getName(i).toString();
    String otherElement = other.getName(j).toString();
    if (!element.equals(otherElement)) {
...
StringfileName(final Path path)
Get just the filename part of the path
return path.getFileName().toString();