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

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

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

FileUtils.toFile介绍

[英]Convert from a URL to a File.

From version 1.1 this method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt. Starting with version 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. Additionally, malformed percent-encoded octets are handled leniently by passing them through literally.
[中]从URL转换为File
从版本1.1开始,此方法将解码URL。file:///my%20docs/file.txt等语法将正确解码为/my docs/file.txt。从版本1.5开始,此方法使用UTF-8将百分比编码的八位字节解码为字符。此外,格式错误的百分比编码八位字节可以通过字面上的传递来轻松处理。

代码示例

代码示例来源:origin: SonarSource/sonarqube

private static File getResource(String path) {
  String resourcePath = path;
  if (!resourcePath.startsWith("/")) {
   resourcePath = "/" + resourcePath;
  }
  URL url = TestUtils.class.getResource(resourcePath);
  if (url != null) {
   return FileUtils.toFile(url);
  }
  return null;
 }
}

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

private FileInputStream getFileInputStream( URL url ) throws FileNotFoundException {
 return new FileInputStream( FileUtils.toFile( url ) );
}

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

"URL could not be converted to a File: " + url);
files[i] = toFile(url);

代码示例来源:origin: SonarSource/sonarqube

private File getFile(String filename) {
  return FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/" + filename));
 }
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
 *
 * @param path the starting slash is optional
 * @return the resource. Null if resource not found
 */
public static File getResource(String path) {
 String resourcePath = path;
 if (!resourcePath.startsWith("/")) {
  resourcePath = "/" + resourcePath;
 }
 URL url = ProjectReactorBuilderTest.class.getResource(resourcePath);
 if (url != null) {
  return FileUtils.toFile(url);
 }
 return null;
}

代码示例来源:origin: SonarSource/sonarqube

/**
  * Get the artifact of plugins stored in src/test/projects
  */
 public static File jarOf(String dirName) {
  File target = FileUtils.toFile(TestProjectUtils.class.getResource(String.format("/%s/target/", dirName)));
  Collection<File> jars = FileUtils.listFiles(target, new String[] {"jar"}, false);
  if (jars == null || jars.size() != 1) {
   throw new IllegalArgumentException("Test project is badly defined: " + dirName);
  }
  return jars.iterator().next();
 }
}

代码示例来源:origin: SonarSource/sonarqube

private void downloadRelease(Release release) throws URISyntaxException, IOException {
 String url = release.getDownloadUrl();
 URI uri = new URI(url);
 if (url.startsWith("file:")) {
  // used for tests
  File file = toFile(uri.toURL());
  copyFileToDirectory(file, downloadDir);
 } else {
  String filename = substringAfterLast(uri.getPath(), "/");
  if (!filename.endsWith("." + PLUGIN_EXTENSION)) {
   filename = release.getKey() + "-" + release.getVersion() + "." + PLUGIN_EXTENSION;
  }
  File targetFile = new File(downloadDir, filename);
  File tempFile = new File(downloadDir, filename + "." + TMP_SUFFIX);
  downloader.download(uri, tempFile);
  copyFile(tempFile, targetFile);
  deleteQuietly(tempFile);
 }
}

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

@Test
public void testToFile1() throws Exception {
  final URL url = new URL("file", null, "a/b/c/file.txt");
  final File file = FileUtils.toFile(url);
  assertTrue(file.toString().contains("file.txt"));
}

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

@Test
public void testToFile2() throws Exception {
  final URL url = new URL("file", null, "a/b/c/file%20n%61me%2520.tx%74");
  final File file = FileUtils.toFile(url);
  assertTrue(file.toString().contains("file name%20.txt"));
}

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

@Test
public void testToFile4() throws Exception {
  final URL url = new URL("file", null, "a/b/c/file%%20%me.txt%");
  final File file = FileUtils.toFile(url);
  assertTrue(file.toString().contains("file% %me.txt%"));
}

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

@Test
public void testToFile3() throws Exception {
  assertEquals(null, FileUtils.toFile(null));
  assertEquals(null, FileUtils.toFile(new URL("http://jakarta.apache.org")));
}

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

@Test
public void testToFileUtf8() throws Exception {
  final URL url = new URL("file", null, "/home/%C3%A4%C3%B6%C3%BC%C3%9F");
  final File file = FileUtils.toFile(url);
  assertTrue(file.toString().contains("\u00E4\u00F6\u00FC\u00DF"));
}

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

@Test
public void testToFile5() throws Exception {
  final URL url = new URL("file", null, "both%20are%20100%20%25%20true");
  final File file = FileUtils.toFile(url);
  assertEquals("both are 100 % true", file.toString());
}

代码示例来源:origin: SonarSource/sonarqube

private File loadFile(String filename) throws IOException {
 File src = FileUtils.toFile(getClass().getResource(getClass().getSimpleName() + "/" + filename));
 File dest = new File(temp.newFolder(), filename);
 FileUtils.copyFile(src, dest);
 return dest;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void create_from_file() {
 File checkstyleJar = FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/sonar-checkstyle-plugin-2.8.jar"));
 PluginInfo checkstyleInfo = PluginInfo.create(checkstyleJar);
 assertThat(checkstyleInfo.getName()).isEqualTo("Checkstyle");
 assertThat(checkstyleInfo.getMinimalSqVersion()).isEqualTo(Version.create("2.8"));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzipping_file_extracts_subset_of_files() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt"));
 assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt"));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzipping_creates_target_directory_if_it_does_not_exist() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File tempDir = temp.newFolder();
 Files.delete(tempDir);
 File subDir = new File(tempDir, "subDir");
 ZipUtils.unzip(zip, subDir);
 assertThat(subDir.list()).hasSize(3);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzip_file() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir);
 assertThat(toDir.list()).hasSize(3);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void zip_directory() throws IOException {
 File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt"));
 File dir = foo.getParentFile();
 File zip = temp.newFile();
 ZipUtils.zipDir(dir, zip);
 assertThat(zip).exists().isFile();
 assertThat(zip.length()).isGreaterThan(1L);
 Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries());
 assertThat(zipEntries).hasSize(4);
 File unzipDir = temp.newFolder();
 ZipUtils.unzip(zip, unzipDir);
 assertThat(new File(unzipDir, "bar.txt")).exists().isFile();
 assertThat(new File(unzipDir, "foo.txt")).exists().isFile();
 assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile();
}

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

boolean openFile() throws Exception {
 data.oneFileOpened = true;
 String realFilename = environmentSubstitute( meta.getFilename() );
 if ( log.isBasic() ) {
  logBasic( BaseMessages.getString( PKG, "AccessOutput.log.WritingToFile", realFilename ) );
 }
 FileObject fileObject = KettleVFS.getFileObject( realFilename, getTransMeta() );
 File file = FileUtils.toFile( fileObject.getURL() );
 // First open or create the access file
 if ( !file.exists() ) {
  if ( meta.isFileCreated() ) {
   data.createDatabase( file );
  } else {
   logError( BaseMessages.getString( PKG, "AccessOutput.InitError.FileDoesNotExist", realFilename ) );
   return false;
  }
 } else {
  data.openDatabase( file );
 }
 // Add the filename to the result object...
 //
 if ( meta.isAddToResultFiles() ) {
  ResultFile resultFile =
   new ResultFile( ResultFile.FILE_TYPE_GENERAL, fileObject, getTransMeta().getName(), toString() );
  resultFile.setComment( "This file was created with an access output step" );
  addResultFile( resultFile );
 }
 return true;
}

相关文章

FileUtils类方法