org.apache.commons.io.FilenameUtils.concat()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(16.3k)|赞(0)|评价(0)|浏览(378)

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

FilenameUtils.concat介绍

[英]Concatenates a filename to a base path using normal command line style rules.

The effect is equivalent to resultant directory after changing directory to the first argument, followed by changing directory to the second argument.

The first argument is the base path, the second is the path to concatenate. The returned path is always normalized via #normalize(String), thus .. is handled.

If pathToAdd is absolute (has an absolute prefix), then it will be normalized and returned. Otherwise, the paths will be joined, normalized and returned.

The output will be the same on both Unix and Windows except for the separator character.

/foo/ + bar          -->   /foo/bar 
/foo + bar           -->   /foo/bar 
/foo + /bar          -->   /bar 
/foo + C:/bar        -->   C:/bar 
/foo + C:bar         -->   C:bar (*) 
/foo/a/ + ../bar     -->   foo/bar 
/foo/ + ../../bar    -->   null 
/foo/ + /bar         -->   /bar 
/foo/.. + /bar       -->   /bar 
/foo + bar/c.txt     -->   /foo/bar/c.txt 
/foo/c.txt + bar     -->   /foo/c.txt/bar (!)

(*) Note that the Windows relative drive prefix is unreliable when used with this method. (!) Note that the first parameter must be a path. If it ends with a name, then the name will be built into the concatenated path. If this might be a problem, use #getFullPath(String) on the base path argument.
[中]使用普通命令行样式规则将文件名连接到基本路径。
其效果相当于将directory更改为第一个参数,然后将directory更改为第二个参数后的结果目录。
第一个参数是基本路径,第二个参数是要连接的路径。返回的路径总是通过#normalize(String)进行规范化,因此..被处理。
如果pathToAdd是绝对的(有绝对前缀),那么它将被规范化并返回。否则,路径将被连接、规范化并返回。
除了分隔符外,Unix和Windows上的输出将相同。

/foo/ + bar          -->   /foo/bar 
/foo + bar           -->   /foo/bar 
/foo + /bar          -->   /bar 
/foo + C:/bar        -->   C:/bar 
/foo + C:bar         -->   C:bar (*) 
/foo/a/ + ../bar     -->   foo/bar 
/foo/ + ../../bar    -->   null 
/foo/ + /bar         -->   /bar 
/foo/.. + /bar       -->   /bar 
/foo + bar/c.txt     -->   /foo/bar/c.txt 
/foo/c.txt + bar     -->   /foo/c.txt/bar (!)

(*)请注意,与此方法一起使用时,Windows相对驱动器前缀不可靠。(!) 请注意,第一个参数必须是路径。如果以名称结尾,则该名称将构建到连接的路径中。如果这可能是个问题,请在基本路径参数上使用#getFullPath(字符串)。

代码示例

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

protected Resource createClasspathResource(String rootLocation, String propertyName, String suffix) {
  suffix = (StringUtils.isEmpty(suffix)) ? "" : "-" + suffix;
  String fileName = propertyName + suffix + ".properties";
  return new ClassPathResource(FilenameUtils.concat(rootLocation, fileName));
}

代码示例来源:origin: deeplearning4j/dl4j-examples

private void saveEvaluation(boolean train, IEvaluation[] evaluations) throws Exception {
  String evalPath = FilenameUtils.concat(outputPath, ("evaluation_" + (train ? "train" : "test")));
  //Write evaluations to disk
  for( int i=0; i<evaluations.length; i++ ){
    String path = FilenameUtils.concat(evalPath, "evaluation_" + i + ".txt");
    FileUtils.writeStringToFile(new File(path), evaluations[i].stats(), Charset.forName("UTF-8"));
  }
}

代码示例来源:origin: deeplearning4j/dl4j-examples

private void saveEvaluation(boolean train, IEvaluation[] evaluations, JavaSparkContext sc) throws IOException {
  String evalPath = FilenameUtils.concat(outputPath, ("evaluation_" + (train ? "train" : "test")));
  //Write evaluations to disk
  for (int i = 0; i < evaluations.length; i++) {
    String path = FilenameUtils.concat(evalPath, "evaluation_" + System.currentTimeMillis() + "_" + i + ".txt");
    SparkUtils.writeStringToFile(path, evaluations[i].stats(), sc);
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Returns a directory that is unique for this work area. 
 *   
 */
protected String getTempDirectory(String baseDirectory) {
  assert baseDirectory != null;
  Random random = new Random();
  // This code is used to ensure that we don't have thousands of sub-directories in a single parent directory.
  for (int i = 0; i < maxGeneratedDirectoryDepth; i++) {
    if (i == 4) {
      LOG.warn("Property asset.server.max.generated.file.system.directories set to high, currently set to " +
          maxGeneratedDirectoryDepth + " ignoring and only creating 4 levels.");
      break;
    }
    // check next int value
    int num = random.nextInt(256);
    baseDirectory = FilenameUtils.concat(baseDirectory, Integer.toHexString(num));
  }
  return FilenameUtils.concat(baseDirectory, buildThreadIdString());
}

代码示例来源:origin: deeplearning4j/dl4j-examples

public static void generateVideoData(String outputFolder, String filePrefix, int nVideos, int nFrames,
                     int width, int height, int numShapesPerVideo, boolean backgroundNoise,
                     int numDistractorsPerFrame, long seed) throws Exception {
    Random r = new Random(seed);

    for (int i = 0; i < nVideos; i++) {
      String videoPath = FilenameUtils.concat(outputFolder, filePrefix + "_" + i + ".mp4");
      String labelsPath = FilenameUtils.concat(outputFolder, filePrefix + "_" + i + ".txt");
      int[] labels = generateVideo(videoPath, nFrames, width, height, numShapesPerVideo, r, backgroundNoise, numDistractorsPerFrame);

      //Write labels to text file
      StringBuilder sb = new StringBuilder();
      for (int j = 0; j < labels.length; j++) {
        sb.append(labels[j]);
        if (j != labels.length - 1) sb.append("\n");
      }
      Files.write(Paths.get(labelsPath), sb.toString().getBytes("utf-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

@VisibleForTesting
static String findValidTarget( String folderName, final String fileName ) throws KettleException {
 if ( fileName == null || folderName == null ) {
  throw new IllegalArgumentException( "Cannot have null arguments to findValidTarget" );
 }
 String fileNameRoot = FilenameUtils.getBaseName( fileName ), ext = "." + FilenameUtils.getExtension( fileName );
 if ( ( ext.length() == 1 ) ) { // only a "."
  ext = "";
 }
 String rtn = "", base = FilenameUtils.concat( folderName, fileNameRoot );
 int baseSz = base.length();
 StringBuilder build = new StringBuilder( baseSz ).append( base );
 int i = -1;
 do {
  i++;
  build.setLength( baseSz ); // bring string back to size
  build.append( i > 0 ? Integer.toString( i ) : "" ).append( ext );
  rtn = build.toString();
 } while ( KettleVFS.fileExists( rtn ) );
 return rtn;
}

代码示例来源:origin: deeplearning4j/dl4j-examples

protected void saveDatasets(DataSetIterator iterator, String dir) {
  AtomicInteger counter = new AtomicInteger(0);
  new File(dir).mkdirs();
  while (iterator.hasNext()) {
    String path = FilenameUtils.concat(dir, "dataset-" + (counter.getAndIncrement()) + ".bin");
    iterator.next().save(new File(path));
    if (counter.get() % 500 == 0)
      log.info("{} datasets saved so far...", counter.get());
  }
}

代码示例来源:origin: deeplearning4j/dl4j-examples

private void writeConfig() throws Exception {
  long time = System.currentTimeMillis();
  StringBuilder sb = new StringBuilder();
  sb.append("Output Path: ").append(outputPath).append("\n")
      .append("Time: ").append(time).append("\n")
      .append("RNG Seed: ").append(rngSeed).append("\n")
      .append("Total Examples (Train): ").append(totalExamplesTrain).append("\n")
      .append("Total Examples (Test): ").append(totalExamplesTest).append("\n")
      .append("numEpoch: ").append(numEpochs).append("\n")
      .append("BatchSize: ").append(batchSize).append("\n")
      .append("Listener Frequency: ").append(listenerFrequency).append("\n")
      .append("\n");
  String str = sb.toString();
  log.info(str);
  //Write to file:
  String toWrite = sb.toString();
  String path = FilenameUtils.concat(outputPath, "experimentConfig.txt");
  log.info("Writing experiment config and info to file: {}", path);
  FileUtils.writeStringToFile(new File(outputPath, "experimentConfig.txt"), toWrite, Charset.forName("UTF-8"));
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

break;
  resourceName = FilenameUtils.concat(resourceName, fileHash.substring(i * 2, (i + 1) * 2));
return FilenameUtils.concat(resourceName, FilenameUtils.getName(url));

代码示例来源:origin: deeplearning4j/dl4j-examples

private void writeConfig(JavaSparkContext sc) throws Exception {
  long time = System.currentTimeMillis();
  StringBuilder sb = new StringBuilder();
  sb.append("Output Path: ").append(outputPath).append("\n")
      .append("Time: ").append(time).append("\n")
      .append("numEpoch: ").append(numEpochs).append("\n")
      .append("minibatch: ").append(minibatch).append("\n")
      .append("numNodes: ").append(numNodes).append("\n")
      .append("numWorkpersPerNode: ").append(numWorkersPerNode).append("\n")
      .append("Listener Frequency: ").append(listenerFrequency).append("\n")
      .append("Azure Storage Account: ").append(azureStorageAcct).append("\n")
      .append("Gradient threshold: ").append(gradientThreshold).append("\n")
      .append("Controller: ").append(masterIP).append("\n")
      .append("Port: ").append(port).append("\n")
      .append("Network Mask: ").append(networkMask).append("\n")
      .append("Word vectors path: ").append(wordVectorsPath).append("\n")
      .append("Continue training: ").append(continueTraining).append("\n")
      .append("saveFreqSec: ").append(saveFreqSec).append("\n")
      .append("\n");
  sb.append("\n\n")
      .append("Spark Default Parallelism: ").append(sc.defaultParallelism()).append("\n");
  String str = sb.toString();
  log.info(str);
  String path = FilenameUtils.concat(outputPath, "experimentConfig.txt");
  log.info("Writing experiment config and info to file: {}", path);
  SparkUtils.writeStringToFile(path, str, sc);
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
   * Creates a unique directory on the file system for each site.
   * Each site may be in one of 255 base directories.   This model efficiently supports up to 65,000 sites
   * served from a single file system based on most OS systems ability to quickly access files as long
   * as there are not more than 255 directories.
   * 
   * @param The starting directory for local files which must end with a '/';
   */
  protected String getSiteDirectory(String baseDirectory) {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc != null) {
      Site site = brc.getSite();
      if (site != null) {
        String siteDirectory = "site-" + site.getId();
        String siteHash = DigestUtils.md5Hex(siteDirectory);
        String sitePath = FilenameUtils.concat(siteHash.substring(0, 2), siteDirectory);
        return FilenameUtils.concat(baseDirectory, sitePath);
      }
    }

    return baseDirectory;
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it
 * exists or java.tmp.io if that property has not been set.   
 * 
 */
protected String getBaseDirectory(boolean skipSite) {
  String path = "";
  if (StringUtils.isBlank(tempFileSystemBaseDirectory)) {
    path = DEFAULT_STORAGE_DIRECTORY;
  } else {
    path = tempFileSystemBaseDirectory;
  }
  if (!skipSite) {
    // Create site specific directory if Multi-site (all site files will be located in the same directory)
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc != null && brc.getSite() != null) {
      String siteDirectory = "site-" + brc.getSite().getId();
      String siteHash = DigestUtils.md5Hex(siteDirectory);
      path = FilenameUtils.concat(path, siteHash.substring(0, 2));
      path = FilenameUtils.concat(path, siteDirectory);
    }
  }
  return path;
}

代码示例来源:origin: deeplearning4j/dl4j-examples

/**
 * @param dataDirectory the directory of the IMDB review data set
 * @param wordVectors WordVectors object
 * @param batchSize Size of each minibatch for training
 * @param truncateLength If reviews exceed
 * @param train If true: return the training data. If false: return the testing data.
 */
public SentimentExampleIterator(String dataDirectory, WordVectors wordVectors, int batchSize, int truncateLength, boolean train) throws IOException {
  this.batchSize = batchSize;
  this.vectorSize = wordVectors.getWordVector(wordVectors.vocab().wordAtIndex(0)).length;
  File p = new File(FilenameUtils.concat(dataDirectory, "aclImdb/" + (train ? "train" : "test") + "/pos/") + "/");
  File n = new File(FilenameUtils.concat(dataDirectory, "aclImdb/" + (train ? "train" : "test") + "/neg/") + "/");
  positiveFiles = p.listFiles();
  negativeFiles = n.listFiles();
  this.wordVectors = wordVectors;
  this.truncateLength = truncateLength;
  tokenizerFactory = new DefaultTokenizerFactory();
  tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

protected void saveBundle(Resource resource) {
  FileWorkArea tempWorkArea = fileService.initializeWorkArea();
  String fileToSave = FilenameUtils.separatorsToSystem(getResourcePath(resource.getDescription()));
  String tempFilename = FilenameUtils.concat(tempWorkArea.getFilePathLocation(), fileToSave);
  File tempFile = new File(tempFilename);
  if (!tempFile.getParentFile().exists()) {

代码示例来源:origin: commons-io/commons-io

@Test
public void testConcat() {
  assertEquals(null, FilenameUtils.concat("", null));
  assertEquals(null, FilenameUtils.concat(null, null));
  assertEquals(null, FilenameUtils.concat(null, ""));
  assertEquals(null, FilenameUtils.concat(null, "a"));
  assertEquals(SEP + "a", FilenameUtils.concat(null, "/a"));
  assertEquals(null, FilenameUtils.concat("", ":")); // invalid prefix
  assertEquals(null, FilenameUtils.concat(":", "")); // invalid prefix
  assertEquals("f" + SEP, FilenameUtils.concat("", "f/"));
  assertEquals("f", FilenameUtils.concat("", "f"));
  assertEquals("a" + SEP + "f" + SEP, FilenameUtils.concat("a/", "f/"));
  assertEquals("a" + SEP + "f", FilenameUtils.concat("a", "f"));
  assertEquals("a" + SEP + "b" + SEP + "f" + SEP, FilenameUtils.concat("a/b/", "f/"));
  assertEquals("a" + SEP + "b" + SEP + "f", FilenameUtils.concat("a/b", "f"));
  assertEquals("a" + SEP + "f" + SEP, FilenameUtils.concat("a/b/", "../f/"));
  assertEquals("a" + SEP + "f", FilenameUtils.concat("a/b", "../f"));
  assertEquals("a" + SEP + "c" + SEP + "g" + SEP, FilenameUtils.concat("a/b/../c/", "f/../g/"));
  assertEquals("a" + SEP + "c" + SEP + "g", FilenameUtils.concat("a/b/../c", "f/../g"));
  assertEquals("a" + SEP + "c.txt" + SEP + "f", FilenameUtils.concat("a/c.txt", "f"));
  assertEquals(SEP + "f" + SEP, FilenameUtils.concat("", "/f/"));
  assertEquals(SEP + "f", FilenameUtils.concat("", "/f"));
  assertEquals(SEP + "f" + SEP, FilenameUtils.concat("a/", "/f/"));
  assertEquals(SEP + "f", FilenameUtils.concat("a", "/f"));

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

FilenameUtils.concat(clusterConfigRootDir, CLUSTER_CONFIG_ARTIFACTS_DIR_NAME);
this.configDiskDirPath = FilenameUtils.concat(clusterConfigRootDir, configDiskDirName);
this.sharedConfigLockingService = getSharedConfigLockService(cache.getDistributedSystem());
this.status.set(SharedConfigurationStatus.NOT_STARTED);

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

String groupDir = FilenameUtils.concat(this.configDirPath, group);
Set<String> jarNames = new HashSet<>();
for (String jarFullPath : jarFullPaths) {
 File stagedJar = new File(jarFullPath);
 jarNames.add(stagedJar.getName());
 String filePath = FilenameUtils.concat(groupDir, stagedJar.getName());
 File jarFile = new File(filePath);
 FileUtils.copyFile(stagedJar, jarFile);

代码示例来源:origin: deeplearning4j/dl4j-examples

String networkPath = FilenameUtils.concat(saveDirectory, "network.bin");
FileSystem fileSystem = FileSystem.get(sc.hadoopConfiguration());
try (BufferedOutputStream os = new BufferedOutputStream(fileSystem.create(new Path(networkPath)))) {
String evalPath = FilenameUtils.concat(saveDirectory, "evaluation.txt");
SparkUtils.writeStringToFile(evalPath, evaluation.stats(), sc);

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * {@inheritDoc}
 *
 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
 */
@Override
public void widgetSelected( final SelectionEvent event ) {
 final FileDialog dialog = new FileDialog( this.shell, SWT.OPEN );
 dialog.setFilterExtensions( this.filterExtensions );
 dialog.setFilterNames( this.filterNames );
 if ( this.textVar.getText() != null ) {
  dialog.setFileName( this.textVar.getText() );
 }
 if ( dialog.open() != null ) {
  final String filename = FilenameUtils.concat( dialog.getFilterPath(), dialog.getFileName() );
  this.textVar.setText( filename );
 }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

tmpdir = tmpdir + File.separator;
provider.fileSystemBaseDirectory = FilenameUtils.concat(tmpdir, "test");
provider.maxGeneratedDirectoryDepth = 2;
File file = provider.getResource("/product/myproductimage.jpg");

相关文章