本文整理了Java中slash.common.io.Files
类的一些代码示例,展示了Files
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files
类的具体详情如下:
包路径:slash.common.io.Files
类名称:Files
[英]Provides file and file name functionality.
[中]提供文件和文件名功能。
代码示例来源:origin: cpesch/RouteConverter
/**
* @param file the file to find the extension
* @return the extension of the file, which are the characters
* starting with the last dot in the file name in lowercase characters
*/
public static String getExtension(File file) {
return getExtension(file.getName());
}
代码示例来源:origin: cpesch/RouteConverter
public String getUrl() {
return printArrayToDialogString(getHosts().toArray(), false);
}
代码示例来源:origin: cpesch/RouteConverter
static String calculateConvertFileName(File file, int index, int maximum, String extension, int fileNameLength) {
String name = file.getName();
name = removeExtension(name);
name = name.substring(0, min(name.length(), fileNameLength));
if (calculateNumberLength(maximum) > 0) {
String number = numberToString(index, maximum);
name = name.substring(0, min(name.length(), fileNameLength - number.length()));
name += number;
}
name = setExtension(name, extension);
File parentFile = file.getParentFile();
String path = parentFile != null ? parentFile.getPath() : ".";
return new File(path, name).getAbsolutePath();
}
代码示例来源:origin: cpesch/RouteConverter
public static String createReadablePath(URL url) {
File file = toFile(url);
if (file != null)
return createReadablePath(file);
return url.toExternalForm();
}
代码示例来源:origin: cpesch/RouteConverter
public static void recursiveDelete(File path) throws IOException {
File[] files = path.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory())
recursiveDelete(file);
delete(file);
}
}
delete(path);
}
代码示例来源:origin: cpesch/RouteConverter
/**
* Replace the extension of the given file name with the
* given extension.
*
* @param name the file name to replace the extension
* @param extension the new extension for the file name
* @return the file name with the given extension
*/
public static String setExtension(String name, String extension) {
name = removeExtension(name);
name += extension;
return name;
}
代码示例来源:origin: cpesch/RouteConverter
public synchronized void scanMaps() throws IOException {
invokeInAwtEventQueue(() -> availableOfflineMapsModel.clear());
long start = currentTimeMillis();
final File mapsDirectory = getMapsDirectory();
List<File> mapFiles = collectFiles(mapsDirectory, DOT_MAP);
File[] mapFilesArray = mapFiles.toArray(new File[0]);
for (final File file : mapFilesArray) {
// avoid directory with world.map
if(file.getParent().endsWith("routeconverter"))
continue;
checkFile(file);
invokeInAwtEventQueue(() ->
availableOfflineMapsModel.addOrUpdateItem(new VectorMap(removePrefix(mapsDirectory, file), file.toURI().toString(), extractBoundingBox(file), file))
);
}
long end = currentTimeMillis();
log.info(format("Collected %d map files %s from %s in %d milliseconds",
mapFilesArray.length, printArrayToDialogString(mapFilesArray, false), mapsDirectory, (end - start)));
}
代码示例来源:origin: cpesch/RouteConverter
private void saveFiles(File[] files, NavigationFormat format, BaseRoute route,
boolean exportSelectedRoute, boolean confirmOverwrite, boolean openAfterSave) {
final RouteConverter r = RouteConverter.getInstance();
String targetsAsString = printArrayToDialogString(files, true);
startWaitCursor(r.getFrame().getRootPane());
try {
openPositionList(toUrls(files), getNavigationFormatRegistry().getReadFormatsWithPreferredFormat(format));
log.info(format("Open after save: %s", files[0]));
String path = createReadablePath(url);
urlModel.setString(path);
recentUrlsModel.addUrl(url);
代码示例来源:origin: cpesch/RouteConverter
private void doExtract(File tempFile, File destination, boolean flatten) throws IOException {
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(tempFile))) {
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
if (entry.isDirectory()) {
if (!flatten) {
File directory = new File(destination, entry.getName());
handleDirectory(directory, entry);
}
} else {
File extracted;
if(flatten)
extracted = new File(destination, lastPathFragment(entry.getName(), MAX_VALUE));
else {
extracted = new File(destination, entry.getName());
}
File directory = extracted.getParentFile();
handleDirectory(directory, entry);
log.info(format("Extracting from %s to %s", tempFile, extracted));
FileOutputStream output = new FileOutputStream(extracted);
new Copier(listener).copy(zipInputStream, output, 0, entry.getSize());
// do not close zip input stream
closeQuietly(output);
setLastModified(extracted, fromMillis(entry.getTime()));
zipInputStream.closeEntry();
}
entry = zipInputStream.getNextEntry();
}
}
}
代码示例来源:origin: cpesch/RouteConverter
public static String lastPathFragment(String path, int maximumLength) {
return lastPathFragment(path, maximumLength, false);
}
代码示例来源:origin: cpesch/RouteConverter
private File createSelectedTarget() {
File target = new File(urlModel.getString());
target = findExistingPath(target);
NavigationFormat format = formatAndRoutesModel.getFormat();
File path = target != null ? target : new File(preferences.get(WRITE_PATH_PREFERENCE + format.getClass().getSimpleName(), ""));
path = findExistingPath(path);
if (path == null)
path = new File("");
String fileName = path.getName();
if (format instanceof GoPal3RouteFormat)
fileName = createGoPalFileName(fileName);
return new File(calculateConvertFileName(new File(path.getParentFile(), fileName), "", format.getMaximumFileNameLength()));
}
代码示例来源:origin: cpesch/RouteConverter
public static String printArrayToDialogString(Object[] array, boolean shorten) {
if (array == null)
return "null";
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i > 0)
if (i == array.length - 1)
buffer.append(" and\n");
else
buffer.append(",\n");
String string = array[i].toString();
if(shorten)
string = shortenPath(string, 60);
buffer.append("'").append(string).append("'");
}
return buffer.toString();
}
}
代码示例来源:origin: cpesch/RouteConverter
private void populateMenu() {
menu.removeAll();
List<URL> urls = recentUrlsModel.getUrls();
for (URL url : urls) {
File file = toFile(url);
JMenuItem menuItem = new JMenuItem(new ReopenAction(url));
String text = file != null ? file.getAbsolutePath() : url.toExternalForm();
menuItem.setText(shortenPath(text, preferences.getInt(MAXIMUM_REOPEN_URL_MENU_TEXT_LENGTH_PREFERENCE, 80)));
menuItem.setToolTipText(text);
menu.add(menuItem);
}
menu.setEnabled(urls.size() > 0);
}
}
代码示例来源:origin: cpesch/RouteConverter
public void delete() throws IOException {
recursiveDelete(directory);
}
代码示例来源:origin: cpesch/RouteConverter
File source = absolutize(new File(args[0]));
if (!source.exists()) {
log.severe("Source '" + source.getAbsolutePath() + "' does not exist; stopping.");
String baseName = removeExtension(args[2]);
File target = absolutize(new File(baseName + format.getExtension()));
if (target.exists()) {
log.severe("Target '" + target.getAbsolutePath() + "' already exists; stopping.");
代码示例来源:origin: cpesch/RouteConverter
public static void setLastModified(File file, CompactCalendar lastModified) throws IOException {
if (lastModified == null)
return;
setLastModified(file, lastModified.getTimeInMillis());
}
代码示例来源:origin: cpesch/RouteConverter
public static File[] createTargetFiles(File pattern, int fileCount, String extension, int fileNameLength) {
File[] files = new File[fileCount];
if (fileCount == 1) {
files[0] = new File(calculateConvertFileName(pattern, extension, fileNameLength));
} else {
for (int i = 0; i < fileCount; i++) {
files[i] = new File(calculateConvertFileName(pattern, i + 1, fileCount, extension, fileNameLength));
}
}
return files;
}
代码示例来源:origin: cpesch/RouteConverter
public void openFile() {
if (!confirmDiscard())
return;
RouteConverter r = RouteConverter.getInstance();
JFileChooser chooser = createJFileChooser();
chooser.setDialogTitle(RouteConverter.getBundle().getString("open-file-dialog-title"));
setReadFormatFileFilters(chooser);
chooser.setSelectedFile(createSelectedSource());
chooser.setFileSelectionMode(FILES_ONLY);
chooser.setMultiSelectionEnabled(true);
int open = chooser.showOpenDialog(r.getFrame());
if (open != APPROVE_OPTION)
return;
File[] selected = chooser.getSelectedFiles();
if (selected == null || selected.length == 0)
return;
NavigationFormat selectedFormat = getSelectedFormat(chooser.getFileFilter());
setReadFormatFileFilterPreference(selectedFormat);
prepareForNewPositionList();
List<URL> urls = toUrls(selected);
List<NavigationFormat> formats = selectedFormat != null ?
getNavigationFormatRegistry().getReadFormatsWithPreferredFormat(selectedFormat) :
getNavigationFormatRegistry().getReadFormatsPreferredByExtension(getExtension(urls));
openPositionList(urls, formats);
}
代码示例来源:origin: cpesch/RouteConverter
public void update(Category parent, String description) throws IOException {
File category = toFile(new URL(parent.getHref()));
File newName = new File(category, encodeFileName(description));
if (newName.exists())
throw new DuplicateNameException(format("%s %s already exists", newName.isDirectory() ? "Category" : "Route", description), newName.getAbsolutePath());
if (!file.renameTo(newName))
throw new IOException(format("Cannot rename %s to %s", file, newName));
file = newName;
}
代码示例来源:origin: cpesch/RouteConverter
public static String generateChecksum(File file) throws IOException {
try (InputStream inputStream = new FileInputStream(file)) {
return generateChecksum(inputStream);
}
}
内容来源于网络,如有侵权,请联系作者删除!