use of java.nio.file.attribute.FileTime in project sakuli by ConSol.
the class TestSuiteHelperTest method testModifyFiles.
@Test
public void testModifyFiles() throws Exception {
Path path = Paths.get("temp-testsuite.suite");
try {
String source = "line1\r\n\r\nbla\r\n";
FileUtils.writeStringToFile(path.toFile(), source);
FileTime beforeTimeStamp = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS);
Thread.sleep(1100);
String result = TestSuiteHelper.prepareTestSuiteFile(path);
assertEquals(result, "line1\r\n//\r\nbla\r\n");
FileTime afterTimeStamp = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS);
assertNotEquals(beforeTimeStamp, afterTimeStamp);
} finally {
Files.deleteIfExists(path);
}
}
use of java.nio.file.attribute.FileTime in project jabref by JabRef.
the class OpenDatabaseAction method openTheFile.
/**
* @param file the file, may be null or not existing
*/
private void openTheFile(Path file, boolean raisePanel) {
Objects.requireNonNull(file);
if (Files.exists(file)) {
Path fileToLoad = file.toAbsolutePath();
frame.output(Localization.lang("Opening") + ": '" + file + "'");
String fileName = file.getFileName().toString();
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, fileToLoad.getParent().toString());
if (FileBasedLock.hasLockFile(file)) {
Optional<FileTime> modificationTime = FileBasedLock.getLockFileTimeStamp(file);
if ((modificationTime.isPresent()) && ((System.currentTimeMillis() - modificationTime.get().toMillis()) > FileBasedLock.LOCKFILE_CRITICAL_AGE)) {
// The lock file is fairly old, so we can offer to "steal" the file:
int answer = JOptionPane.showConfirmDialog(null, "<html>" + Localization.lang("Error opening file") + " '" + fileName + "'. " + Localization.lang("File is locked by another JabRef instance.") + "<p>" + Localization.lang("Do you want to override the file lock?"), Localization.lang("File locked"), JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
FileBasedLock.deleteLockFile(file);
} else {
return;
}
} else if (!FileBasedLock.waitForFileLock(file)) {
JOptionPane.showMessageDialog(null, Localization.lang("Error opening file") + " '" + fileName + "'. " + Localization.lang("File is locked by another JabRef instance."), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
return;
}
}
if (BackupManager.checkForBackupFile(fileToLoad)) {
BackupUIManager.showRestoreBackupDialog(frame, fileToLoad);
}
ParserResult result;
result = OpenDatabase.loadDatabase(fileToLoad.toString(), Globals.prefs.getImportFormatPreferences());
if (result.getDatabase().isShared()) {
try {
new SharedDatabaseUIManager(frame).openSharedDatabaseFromParserResult(result);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException | NotASharedDatabaseException e) {
// do not open the original file
result.getDatabaseContext().clearDatabaseFile();
result.getDatabase().clearSharedDatabaseID();
LOGGER.error("Connection error", e);
JOptionPane.showMessageDialog(frame, e.getMessage() + "\n\n" + Localization.lang("A local copy will be opened."), Localization.lang("Connection error"), JOptionPane.WARNING_MESSAGE);
}
}
BasePanel panel = addNewDatabase(result, file, raisePanel);
// After adding the database, go through our list and see if
// any post open actions need to be done. For instance, checking
// if we found new entry types that can be imported, or checking
// if the database contents should be modified due to new features
// in this version of JabRef:
final ParserResult finalReferenceToResult = result;
SwingUtilities.invokeLater(() -> OpenDatabaseAction.performPostOpenActions(panel, finalReferenceToResult));
}
}
use of java.nio.file.attribute.FileTime in project bnd by bndtools.
the class IO method copy.
public static Path copy(Path src, Path tgt) throws IOException {
final Path source = src.toAbsolutePath();
final Path target = tgt.toAbsolutePath();
if (Files.isRegularFile(source)) {
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
return tgt;
}
if (Files.isDirectory(source)) {
if (Files.notExists(target)) {
mkdirs(target);
}
if (!Files.isDirectory(target))
throw new IllegalArgumentException("target directory for a directory must be a directory: " + target);
if (target.startsWith(source))
throw new IllegalArgumentException("target directory can not be child of source directory.");
Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new FileVisitor<Path>() {
final FileTime now = FileTime.fromMillis(System.currentTimeMillis());
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path targetdir = target.resolve(source.relativize(dir));
try {
Files.copy(dir, targetdir);
} catch (FileAlreadyExistsException e) {
if (!Files.isDirectory(targetdir))
throw e;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path targetFile = target.resolve(source.relativize(file));
Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING);
Files.setLastModifiedTime(targetFile, now);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
if (exc != null) {
// directory iteration failed
throw exc;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null) {
throw exc;
}
return FileVisitResult.CONTINUE;
}
});
return tgt;
}
throw new FileNotFoundException("During copy: " + source.toString());
}
use of java.nio.file.attribute.FileTime in project Gargoyle by callakrsos.
the class FilePropertiesComposite method apply.
/**********************************************************************************************/
/* 이벤트 처리항목 기술 */
// TODO Auto-generated constructor stub
/**********************************************************************************************/
/**********************************************************************************************/
/*
* (non-Javadoc)
*
* @see java.util.function.Function#apply(java.lang.Object)
*/
@Override
public List<Map<String, Object>> apply(File file) {
ObservableList<Map<String, Object>> items = FXCollections.observableArrayList();
Long totalSpace = this.file.getTotalSpace();
Long usableSpace = this.file.getUsableSpace();
Long freeSpace = this.file.getFreeSpace();
Long fileSize = this.file.length();
String name = this.file.getName();
boolean isExecutable = this.file.canExecute();
boolean isWriable = this.file.canWrite();
boolean isReadable = this.file.canRead();
boolean hidden = this.file.isHidden();
String path = this.file.getAbsolutePath();
int totalCnt = 0;
int fileCnt = 0;
int dirCnt = 0;
if (file.isDirectory()) {
for (File f : file.listFiles()) {
if (f.isFile())
fileCnt++;
else if (f.isDirectory())
dirCnt++;
totalCnt++;
}
}
//grid
items.add(toMap("name", name));
items.add(toMap("path", path));
items.add(toMap("totalSpace(byte)", toNumberString(totalSpace)));
items.add(toMap("usableSpace(byte)", toNumberString(usableSpace)));
items.add(toMap("freeSpace(byte)", toNumberString(freeSpace)));
items.add(toMap("fileSize(byte)", toNumberString(fileSize)));
items.add(toMap("isExecutable", isExecutable));
items.add(toMap("isWriable", isWriable));
items.add(toMap("isReadable", isReadable));
items.add(toMap("hidden", hidden));
//chart
ObservableList<Map<String, Object>> sizeItems = FXCollections.observableArrayList();
sizeItems.add(toMap("totalSpace(byte)", totalSpace.toString()));
sizeItems.add(toMap("usableSpace(byte)", usableSpace.toString()));
sizeItems.add(toMap("freeSpace(byte)", freeSpace.toString()));
sizeItems.add(toMap("fileSize(byte)", fileSize.toString()));
this.picChart.setData(ChartCreator.convertNewData(totalSpace, sizeItems));
try {
BasicFileAttributes readAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
FileTime lastAccessTime = readAttributes.lastAccessTime();
FileTime creationTime = readAttributes.creationTime();
FileTime lastModifiedTime = readAttributes.lastModifiedTime();
boolean symbolicLink = readAttributes.isSymbolicLink();
items.add(toMap("creationTime", toDate(creationTime.toMillis())));
items.add(toMap("lastAccessTime", toDate(lastAccessTime.toMillis())));
items.add(toMap("lastModifiedTime", toDate(lastModifiedTime.toMillis())));
items.add(toMap("symbolicLink", symbolicLink));
} catch (IOException e) {
e.printStackTrace();
}
items.add(toMap("all file count", totalCnt));
items.add(toMap("only file count ", fileCnt));
items.add(toMap("only dir count", dirCnt));
return items;
}
use of java.nio.file.attribute.FileTime in project cassandra by apache.
the class SSTableRepairedAtSetter method main.
/**
* @param args a list of sstables whose metadata we are changing
*/
public static void main(final String[] args) throws IOException {
PrintStream out = System.out;
if (args.length == 0) {
out.println("This command should be run with Cassandra stopped!");
out.println("Usage: sstablerepairedset [--is-repaired | --is-unrepaired] [-f <sstable-list> | <sstables>]");
System.exit(1);
}
if (args.length < 3 || !args[0].equals("--really-set") || (!args[1].equals("--is-repaired") && !args[1].equals("--is-unrepaired"))) {
out.println("This command should be run with Cassandra stopped, otherwise you will get very strange behavior");
out.println("Verify that Cassandra is not running and then execute the command like this:");
out.println("Usage: sstablerepairedset --really-set [--is-repaired | --is-unrepaired] [-f <sstable-list> | <sstables>]");
System.exit(1);
}
Util.initDatabaseDescriptor();
boolean setIsRepaired = args[1].equals("--is-repaired");
List<String> fileNames;
if (args[2].equals("-f")) {
fileNames = Files.readAllLines(Paths.get(args[3]), Charset.defaultCharset());
} else {
fileNames = Arrays.asList(args).subList(2, args.length);
}
for (String fname : fileNames) {
Descriptor descriptor = Descriptor.fromFilename(fname);
if (!descriptor.version.isCompatible()) {
System.err.println("SSTable " + fname + " is in a old and unsupported format");
continue;
}
if (setIsRepaired) {
FileTime f = Files.getLastModifiedTime(new File(descriptor.filenameFor(Component.DATA)).toPath());
descriptor.getMetadataSerializer().mutateRepaired(descriptor, f.toMillis(), null);
} else {
descriptor.getMetadataSerializer().mutateRepaired(descriptor, 0, null);
}
}
}
Aggregations