本文整理了Java中org.scijava.util.FileUtils
类的一些代码示例,展示了FileUtils
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils
类的具体详情如下:
包路径:org.scijava.util.FileUtils
类名称:FileUtils
[英]Useful methods for working with file paths.
[中]使用文件路径的有用方法。
代码示例来源:origin: org.scijava/scijava-common
/**
* Recursively lists the contents of the referenced directory. Directories are
* excluded from the result. Supported protocols include {@code file} and
* {@code jar}.
*
* @param directory The directory whose contents should be listed.
* @return A collection of {@link URL}s representing the directory's contents.
* @see #listContents(URL, boolean, boolean)
*/
public static Collection<URL> listContents(final URL directory) {
return listContents(directory, true, true);
}
代码示例来源:origin: org.scijava/scijava-common
/**
* Converts the given {@link URL} to its corresponding {@link File}.
* <p>
* This method is similar to calling {@code new File(url.toURI())} except that
* it also handles "jar:file:" URLs, returning the path to the JAR file.
* </p>
*
* @param url The URL to convert.
* @return A file path suitable for use with e.g. {@link FileInputStream}
* @throws IllegalArgumentException if the URL does not correspond to a file.
*/
public static File urlToFile(final URL url) {
return url == null ? null : urlToFile(url.toString());
}
代码示例来源:origin: org.scijava/minimaven
private final void deleteVersions(final File directory, final String filename,
final File excluding)
{
final File[] versioned = FileUtils.getAllVersions(directory, filename);
if (versioned == null) return;
for (final File file : versioned) {
if (file.equals(excluding)) continue;
if (!file.getName().equals(filename)) {
env.err.println("Warning: deleting '" + file + "'");
}
if (!file.delete()) {
env.err.println("Warning: could not delete '" + file + "'");
}
}
}
代码示例来源:origin: org.scijava/scijava-common
/** Helper method of {@link #findResources(String, Iterable)}. */
private static void getResources(final Pattern pattern,
final Map<String, URL> result, final URL base)
{
final String prefix = urlPath(base);
if (prefix == null) return; // unsupported base URL
for (final URL url : FileUtils.listContents(base)) {
final String s = urlPath(url);
if (s == null || !s.startsWith(prefix)) continue;
if (pattern == null || pattern.matcher(s).matches()) {
// this resource matches the pattern
final String key = urlPath(s.substring(prefix.length()));
if (key != null) result.put(key, url);
}
}
}
代码示例来源:origin: org.scijava/scijava-common
@Override
public String getVersion() {
final File file = new File(path);
if (!file.exists()) return null; // no version for non-existent script
try {
return DigestUtils.bestHex(FileUtils.readFile(file));
}
catch (final IOException exc) {
log.error(exc);
}
final Date lastModified = FileUtils.getModifiedTime(file);
final String datestamp =
new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(lastModified);
return datestamp;
}
代码示例来源:origin: net.imagej/imagej-legacy
for (final URL url : FileUtils.listContents(pomBase, true, true)) {
if (url.toExternalForm().endsWith("/pom.xml")) {
return new POM(url);
final File file = FileUtils.urlToFile(location);
final File baseDir = AppUtils.getBaseDirectory(file, null);
final File pomFile = new File(baseDir, "pom.xml");
代码示例来源:origin: sc.fiji/fiji-compat
/**
* Returns the file extension of a given file
*
* @param path the path to the file
* @return the file extension
* @deprecated see {@link FileUtils#getExtension(String)}
*/
public static String getFileExtension(String path) {
return FileUtils.getExtension(path);
}
代码示例来源:origin: scijava/scijava-common
@Test
public void testUnpackedClass() throws IOException {
final File tmpDir = createTemporaryDirectory("class-utils-test-");
final String path = getClass().getName().replace('.', '/') + ".class";
final File classFile = new File(tmpDir, path);
assertTrue(classFile.getParentFile().exists() ||
classFile.getParentFile().mkdirs());
copy(getClass().getResource("/" + path).openStream(),
new FileOutputStream(classFile), true);
final ClassLoader classLoader =
new URLClassLoader(new URL[] { tmpDir.toURI().toURL() }, null);
final URL location = ClassUtils.getLocation(getClass().getName(),
classLoader);
assertEquals(tmpDir, FileUtils.urlToFile(location));
FileUtils.deleteRecursively(tmpDir);
}
代码示例来源:origin: org.scijava/scijava-common
@Override
public String loadChecksum(final Location source) throws IOException {
final File cachedChecksum = cachedChecksum(source);
if (!cachedChecksum.exists()) return null;
return DigestUtils.string(FileUtils.readFile(cachedChecksum));
}
代码示例来源:origin: scijava/scijava-common
FileUtils.writeFile(inFile, data);
final byte[] cachedData = FileUtils.readFile(cachedFile.getFile());
assertArrayEquals(data, cachedData);
FileUtils.deleteRecursively(cacheDir);
代码示例来源:origin: scijava/scijava-common
@Test
public void testGetAllVersions() throws IOException {
final String withClassifier = "miglayout-3.7.3.1-swing.jar";
final String withoutClassifier = "miglayout-3.7.3.1.jar";
final File tmp = createTemporaryDirectory("delete-other-");
try {
writeEmptyFile(new File(tmp, withClassifier));
writeEmptyFile(new File(tmp, withoutClassifier));
assertArrayEquals(new File[] { new File(tmp, withClassifier) },
FileUtils.getAllVersions(tmp, withClassifier));
assertArrayEquals(new File[] { new File(tmp, withoutClassifier) },
FileUtils.getAllVersions(tmp, withoutClassifier));
}
finally {
FileUtils.deleteRecursively(tmp);
}
}
代码示例来源:origin: org.scijava/scijava-common
final String protocol = directory.getProtocol();
if (protocol.equals("file")) {
final File dir = urlToFile(directory);
final File[] list = dir.listFiles();
if (list != null) {
appendContents(result, file.toURI().toURL(), recurse, filesOnly);
代码示例来源:origin: scijava/scijava-common
@Test
public void testDownload() throws IOException, InterruptedException,
ExecutionException
{
final byte[] data = randomBytes(0xbabebabe);
final String prefix = getClass().getName();
final File inFile = File.createTempFile(prefix, "testDownloadIn");
final File outFile = File.createTempFile(prefix, "testDownloadOut");
try {
FileUtils.writeFile(inFile, data);
final Location src = new FileLocation(inFile);
final Location dest = new FileLocation(outFile);
final Download download = downloadService.download(src, dest);
download.task().waitFor();
final byte[] result = FileUtils.readFile(outFile);
assertArrayEquals(data, result);
}
finally {
inFile.delete();
outFile.delete();
}
}
代码示例来源:origin: org.scijava/scijava-common
/**
* Creates a temporary directory.
* <p>
* Since there is no atomic operation to do that, we create a temporary file,
* delete it and create a directory in its place. To avoid race conditions, we
* use the optimistic approach: if the directory cannot be created, we try to
* obtain a new temporary file rather than erroring out.
* </p>
* <p>
* It is the caller's responsibility to make sure that the directory is
* deleted; see {@link #deleteRecursively(File)}.
* </p>
*
* @param prefix The prefix string to be used in generating the file's name;
* see {@link File#createTempFile(String, String, File)}
* @return An abstract pathname denoting a newly-created empty directory
* @throws IOException
*/
public static File createTemporaryDirectory(final String prefix)
throws IOException
{
return createTemporaryDirectory(prefix, null, null);
}
代码示例来源:origin: org.scijava/scripting-java
/**
* Cleans up the project, if it was only temporary.
*/
private void cleanup() {
if (err != null) err.close();
if (err != null) err.close();
if (temporaryDirectory != null &&
!FileUtils.deleteRecursively(temporaryDirectory))
{
temporaryDirectory.deleteOnExit();
}
}
}
代码示例来源:origin: scijava/scijava-common
/** Tests {@link ScriptInfo#getVersion()}. */
@Test
public void testVersion() throws IOException {
final String script = "" + //
"% @LogService log\n" + //
"% @OUTPUT int output";
// write script to a temporary directory on disk
final File tmpDir = TestUtils.createTemporaryDirectory("script-info-test-");
final String path = "hello.bsizes";
final File scriptFile = new File(tmpDir, path);
FileUtils.writeFile(scriptFile, DigestUtils.bytes(script));
// verify that the version is correct
final ScriptInfo info = new ScriptInfo(context, scriptFile);
final String version = info.getVersion();
final String sha1 = "28f4a2880d604774ac5d604d35f431047a087c9e";
assertTrue(version.matches("^" + sha1 + "$"));
// clean up the temporary directory
FileUtils.deleteRecursively(tmpDir);
}
代码示例来源:origin: org.scijava/scijava-common
/** Scans classpath resources for scripts (e.g., inside JAR files). */
private int scanResources(final List<ScriptInfo> scripts, final Set<URL> urls) {
if (pathPrefix == null) return 0;
// NB: We leave the baseDirectory argument null, because scripts on disk
// will be picked up in the subsequent logic, which handles multiple
// script directories rather than being limited to a single one.
final Map<String, URL> scriptMap = //
FileUtils.findResources(null, pathPrefix, null);
return createInfos(scripts, urls, scriptMap, null);
}
代码示例来源:origin: org.scijava/scijava-common
/**
* Gets the absolute path to the given file, with the directory separator
* standardized to forward slash, like most platforms use.
*
* @param file The file whose path will be obtained and standardized.
* @return The file's standardized absolute path.
*/
public static String getPath(final File file) {
final String path = file.getAbsolutePath();
final String slash = System.getProperty("file.separator");
return getPath(path, slash);
}
代码示例来源:origin: org.scijava/scijava-common
/** Shortens the given path to ensure it conforms to a maximum length. */
private String shortPath(final String path) {
// TODO - shorten path name as needed
return FileUtils.limitPath(path, MAX_DISPLAY_LENGTH);
}
代码示例来源:origin: org.scijava/scijava-search
for (final URL url : FileUtils.listContents(pomBase, true, true)) {
if (url.toExternalForm().endsWith("/pom.xml")) {
return new POM(url);
final File file = FileUtils.urlToFile(location);
final File baseDir = AppUtils.getBaseDirectory(file, null);
final File pomFile = new File(baseDir, "pom.xml");
内容来源于网络,如有侵权,请联系作者删除!