org.apache.nifi.util.file.FileUtils类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.9k)|赞(0)|评价(0)|浏览(195)

本文整理了Java中org.apache.nifi.util.file.FileUtils类的一些代码示例,展示了FileUtils类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils类的具体详情如下:
包路径:org.apache.nifi.util.file.FileUtils
类名称:FileUtils

FileUtils介绍

[英]A utility class containing a few useful static methods to do typical IO operations.
[中]一个实用程序类,包含一些用于执行典型IO操作的有用静态方法。

代码示例

代码示例来源:origin: apache/nifi

/**
 * Deletes the given file. If the given file exists but could not be deleted this will be printed as a warning to the given logger
 *
 * @param file the file to delete
 * @param logger the logger to provide logging information to about the operation
 * @return true if given file no longer exists
 */
public static boolean deleteFile(final File file, final Logger logger) {
  return FileUtils.deleteFile(file, logger, 1);
}

代码示例来源:origin: apache/nifi

private void syncWithRestoreDirectory() throws IOException {
  // sanity check that restore directory is a directory, creating it if necessary
  FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);
  // check that restore directory is not the same as the primary directory
  if (config.getParentFile().getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
    throw new IllegalStateException(
        String.format("Cluster firewall configuration file '%s' cannot be in the restore directory '%s' ",
            config.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
  }
  // the restore copy will have same file name, but reside in a different directory
  final File restoreFile = new File(restoreDirectory, config.getName());
  // sync the primary copy with the restore copy
  FileUtils.syncWithRestore(config, restoreFile, logger);
}

代码示例来源:origin: apache/nifi

} while (bytesWritten < fileSize);
out.force(false);
FileUtils.closeQuietly(fos);
FileUtils.closeQuietly(fis);
fos = null;
fis = null;
if (move && !FileUtils.deleteFile(source, null, 5)) {
  if (logger == null) {
    FileUtils.deleteFile(destination, null, 5);
    throw new IOException("Could not remove file " + source.getAbsolutePath());
  } else {
FileUtils.releaseQuietly(inLock);
FileUtils.releaseQuietly(outLock);
FileUtils.closeQuietly(fos);
FileUtils.closeQuietly(fis);

代码示例来源:origin: apache/nifi

public static void deleteFile(final File file, final boolean recurse) throws IOException {
  if (file.isDirectory() && recurse) {
    FileUtils.deleteFiles(Arrays.asList(file.listFiles()), recurse);
  }
  //now delete the file itself regardless of whether it is plain file or a directory
  if (!FileUtils.deleteFile(file, null, 5)) {
    throw new IOException("Unable to delete " + file.getAbsolutePath());
  }
}

代码示例来源:origin: apache/nifi

/**
 * Syncs a primary copy of a file with the copy in the restore directory. If the restore directory does not have a file and the primary has a file, the the primary's file is copied to the restore
 * directory. Else if the restore directory has a file, but the primary does not, then the restore's file is copied to the primary directory. Else if the primary file is different than the restore
 * file, then an IllegalStateException is thrown. Otherwise, if neither file exists, then no syncing is performed.
 *
 * @param primaryFile the primary file
 * @param restoreFile the restore file
 * @param logger a logger
 * @throws IOException if an I/O problem was encountered during syncing
 * @throws IllegalStateException if the primary and restore copies exist but are different
 */
public static void syncWithRestore(final File primaryFile, final File restoreFile, final Logger logger)
    throws IOException {
  if (primaryFile.exists() && !restoreFile.exists()) {
    // copy primary file to restore
    copyFile(primaryFile, restoreFile, false, false, logger);
  } else if (restoreFile.exists() && !primaryFile.exists()) {
    // copy restore file to primary
    copyFile(restoreFile, primaryFile, false, false, logger);
  } else if (primaryFile.exists() && restoreFile.exists() && !isSame(primaryFile, restoreFile)) {
    throw new IllegalStateException(String.format("Primary file '%s' is different than restore file '%s'",
        primaryFile.getAbsoluteFile(), restoreFile.getAbsolutePath()));
  }
}

代码示例来源:origin: apache/nifi

if (restoreDirectory != null) {
  FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);
    FileUtils.syncWithRestore(tenantsFile, restoreTenantsFile, logger);
  } catch (final IOException | IllegalStateException ioe) {
    throw new AuthorizerCreationException(ioe);
  FileUtils.copyFile(tenantsFile, restoreTenantsFile, false, false, logger);

代码示例来源:origin: apache/nifi

fos.close();
if (!FileUtils.deleteFile(file, null, 5)) {
  throw new IOException("Failed to delete file after shredding");
FileUtils.closeQuietly(fos);

代码示例来源:origin: apache/nifi

/**
 * Deletes all files (not directories) in the given directory (recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
 *
 * @param directory the directory to scan
 * @param filter if null then no filter is used
 * @param logger the logger
 * @param recurse whether to recurse subdirectories or not
 * @param deleteEmptyDirectories default is false; if true will delete directories found that are empty
 */
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) {
  // ensure the specified directory is actually a directory and that it exists
  if (null != directory && directory.isDirectory()) {
    final File ingestFiles[] = directory.listFiles();
    for (File ingestFile : ingestFiles) {
      boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
      if (ingestFile.isFile() && process) {
        FileUtils.deleteFile(ingestFile, logger, 3);
      }
      if (ingestFile.isDirectory() && recurse) {
        FileUtils.deleteFilesInDir(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
        if (deleteEmptyDirectories && ingestFile.list().length == 0) {
          FileUtils.deleteFile(ingestFile, logger, 3);
        }
      }
    }
  }
}

代码示例来源:origin: apache/nifi

public static long copyBytes(final byte[] bytes, final File destination, final boolean lockOutputFile) throws FileNotFoundException, IOException {
  FileOutputStream fos = null;
  FileLock outLock = null;
  long fileSize = 0L;
  try {
    fos = new FileOutputStream(destination);
    final FileChannel out = fos.getChannel();
    if (lockOutputFile) {
      outLock = out.tryLock(0, Long.MAX_VALUE, false);
      if (null == outLock) {
        throw new IOException("Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
      }
    }
    fos.write(bytes);
    fos.flush();
    fileSize = bytes.length;
  } finally {
    FileUtils.releaseQuietly(outLock);
    FileUtils.closeQuietly(fos);
  }
  return fileSize;
}

代码示例来源:origin: apache/nifi

@Override
public long getContainerUsableSpace(String containerName) throws IOException {
  final Path path = containers.get(containerName);
  if (path == null) {
    throw new IllegalArgumentException("No container exists with name " + containerName);
  }
  return FileUtils.getContainerUsableSpace(path);
}

代码示例来源:origin: apache/nifi

@Override
public long getContainerCapacity(final String containerName) throws IOException {
  final Path path = containers.get(containerName);
  if (path == null) {
    throw new IllegalArgumentException("No container exists with name " + containerName);
  }
  long capacity = FileUtils.getContainerCapacity(path);
  if(capacity==0) {
    throw new IOException("System returned total space of the partition for " + containerName + " is zero byte. "
        + "Nifi can not create a zero sized FileSystemRepository.");
  }
  return capacity;
}

代码示例来源:origin: apache/nifi

protected File getBootstrapFile(final Logger logger, String directory, String defaultDirectory, String fileName) throws IOException {
  final File confDir = bootstrapConfigFile.getParentFile();
  final File nifiHome = confDir.getParentFile();
  String confFileDir = System.getProperty(directory);
  final File fileDir;
  if (confFileDir != null) {
    fileDir = new File(confFileDir.trim());
  } else {
    fileDir = new File(nifiHome, defaultDirectory);
  }
  FileUtils.ensureDirectoryExistAndCanAccess(fileDir);
  final File statusFile = new File(fileDir, fileName);
  logger.debug("Status File: {}", statusFile);
  return statusFile;
}

代码示例来源:origin: apache/nifi

@Override
public synchronized void save(final InputStream is) throws IOException {
  try (final OutputStream outStream = Files.newOutputStream(flowXmlPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
      final OutputStream gzipOut = new GZIPOutputStream(outStream)) {
    FileUtils.copy(is, gzipOut);
  }
}

代码示例来源:origin: apache/nifi

/**
 * Deletes all files (not directories..) in the given directory (non recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given
 * logger.
 *
 * @param directory the directory to scan for files to delete
 * @param filter if null then no filter is used
 * @param logger the logger to use
 */
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger) {
  FileUtils.deleteFilesInDir(directory, filter, logger, false);
}

代码示例来源:origin: apache/nifi

public SSLContextFactory(final NiFiProperties properties) throws NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyStoreException, UnrecoverableKeyException {
  keystore = properties.getProperty(NiFiProperties.SECURITY_KEYSTORE);
  keystorePass = getPass(properties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD));
  keystoreType = properties.getProperty(NiFiProperties.SECURITY_KEYSTORE_TYPE);
  truststore = properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE);
  truststorePass = getPass(properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD));
  truststoreType = properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_TYPE);
  // prepare the keystore
  final KeyStore keyStore = KeyStoreUtils.getKeyStore(keystoreType);
  final FileInputStream keyStoreStream = new FileInputStream(keystore);
  try {
    keyStore.load(keyStoreStream, keystorePass);
  } finally {
    FileUtils.closeQuietly(keyStoreStream);
  }
  final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keyStore, keystorePass);
  // prepare the truststore
  final KeyStore trustStore = KeyStoreUtils.getTrustStore(truststoreType);
  final FileInputStream trustStoreStream = new FileInputStream(truststore);
  try {
    trustStore.load(trustStoreStream, truststorePass);
  } finally {
    FileUtils.closeQuietly(trustStoreStream);
  }
  final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(trustStore);
  keyManagers = keyManagerFactory.getKeyManagers();
  trustManagers = trustManagerFactory.getTrustManagers();
}

代码示例来源:origin: apache/nifi

/**
 * Renames the given file from the source path to the destination path. This handles multiple attempts. This should only be used to rename within a given directory. Renaming across directories
 * might not work well. See the <code>File.renameTo</code> for more information.
 *
 * @param source the file to rename
 * @param destination the file path to rename to
 * @param maxAttempts the max number of attempts to attempt the rename
 * @throws IOException if rename isn't successful
 */
public static void renameFile(final File source, final File destination, final int maxAttempts) throws IOException {
  FileUtils.renameFile(source, destination, maxAttempts, false);
}

代码示例来源:origin: apache/nifi

/**
 * Copies the given source file to the given destination file. The given destination will be overwritten if it already exists.
 *
 * @param source the file to copy from
 * @param destination the file to copy to
 * @param lockInputFile if true will lock input file during copy; if false will not
 * @param lockOutputFile if true will lock output file during copy; if false will not
 * @param logger the logger to use
 * @return long number of bytes copied
 * @throws FileNotFoundException if the source file could not be found
 * @throws IOException if unable to read or write to file
 * @throws SecurityException if a security manager denies the needed file operations
 */
public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final Logger logger) throws FileNotFoundException, IOException {
  return FileUtils.copyFile(source, destination, lockInputFile, lockOutputFile, false, logger);
}

代码示例来源:origin: apache/nifi

if (restoreDirectory != null) {
  FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);
    FileUtils.syncWithRestore(authorizationsFile, restoreAuthorizationsFile, logger);
  } catch (final IOException | IllegalStateException ioe) {
    throw new AuthorizerCreationException(ioe);
  FileUtils.copyFile(authorizationsFile, restoreAuthorizationsFile, false, false, logger);

代码示例来源:origin: org.apache.nifi/nifi-utils

fos.close();
if (!FileUtils.deleteFile(file, null, 5)) {
  throw new IOException("Failed to delete file after shredding");
FileUtils.closeQuietly(fos);

代码示例来源:origin: org.apache.nifi/nifi-utils

/**
 * Deletes all files (not directories) in the given directory (recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
 *
 * @param directory the directory to scan
 * @param filter if null then no filter is used
 * @param logger the logger
 * @param recurse whether to recurse subdirectories or not
 * @param deleteEmptyDirectories default is false; if true will delete directories found that are empty
 */
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) {
  // ensure the specified directory is actually a directory and that it exists
  if (null != directory && directory.isDirectory()) {
    final File ingestFiles[] = directory.listFiles();
    for (File ingestFile : ingestFiles) {
      boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
      if (ingestFile.isFile() && process) {
        FileUtils.deleteFile(ingestFile, logger, 3);
      }
      if (ingestFile.isDirectory() && recurse) {
        FileUtils.deleteFilesInDir(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
        if (deleteEmptyDirectories && ingestFile.list().length == 0) {
          FileUtils.deleteFile(ingestFile, logger, 3);
        }
      }
    }
  }
}

相关文章