List of usage examples for java.io File setLastModified
public boolean setLastModified(long time)
From source file:de.uzk.hki.da.pkg.CopyUtility.java
/** * Copies a file to a target folder/*from w w w . j av a 2 s. c o m*/ * * @param source The source file * @param destination The target file * @return false if the SIP creation process was aborted during the copy process, otherwise true * @throws IOException * @throws IllegalArgumentException */ private boolean copy(File source, File destination) throws IOException, IllegalArgumentException { if (sipBuildingProcess.isAborted()) return false; if (source.isDirectory()) { if (!(source.getName().equals("thumbs")) && !(source.getName().equals("thumbnails"))) { if (!destination.exists()) { destination.mkdir(); } String files[] = source.list(); for (String file : files) { File srcFile = new File(source, file); File destFile = new File(destination, file); if (!copy(srcFile, destFile)) return false; } destination.setLastModified(source.lastModified()); } } else { if (!source.getName().equals(".DS_Store") && (source.length() != 0) && checkFileExtension(source.getName())) { FileUtils.copyFile(source, destination); } if (progressManager != null) progressManager.copyProgress(jobId, FileUtils.sizeOf(source)); } return true; }
From source file:org.isatools.isatab.export.DataFilesDispatcher.java
/** * Dispatches a file to the submission repository, where meta-data files are supposed to go. *//*from w ww . j a v a 2 s. com*/ private void dispatchFileToSubmissionRepo(final Study study, String fieldName, String srcFileRelativePath) throws IOException { // Get the file from the location manager String targetPath = dataLocationMgr.buildISATabMetaDataLocation(study); if (targetPath == null) { log.debug( "No META-data file location defined, skipping files copy, probably you need to edit data_locations.xml"); return; } srcFileRelativePath = StringUtils.trimToNull(srcFileRelativePath); if (srcFileRelativePath == null) { String msg = "WARNING: Empty value for file of type '" + fieldName + "', skipping copy"; log.error(msg); return; } String srcFilePath = sourcePath + "/" + srcFileRelativePath; File srcFile = new File(srcFilePath); File targetDir = new File(targetPath); String targetFilePath = targetPath + "/" + srcFileRelativePath; File targetFile = new File(targetFilePath); if (!targetDir.exists()) { log.info("Creating the submission directory: '" + targetPath + "'"); FileUtils.forceMkdir(targetDir); } log.trace("Copying " + srcFile.getPath() + " to '" + targetFilePath + "'..."); FileUtils.copyFile(srcFile, targetFile, true); // needed, there's a bug in the previous function targetFile.setLastModified(srcFile.lastModified()); log.trace("...done"); String backupTargetPath = targetPath + "/original"; String backupFilePath = backupTargetPath + "/" + srcFileRelativePath; File backupFile = new File(backupFilePath); log.trace("Copying " + srcFile.getPath() + " to '" + backupFilePath + "'..."); FileUtils.copyFile(srcFile, backupFile, true); // needed, there's a bug in the previous function backupFile.setLastModified(srcFile.lastModified()); log.trace("...done"); log.info("'" + srcFile.getPath() + "' copied to '" + targetFilePath + "' (and to original/)"); return; }
From source file:com.otway.picasasync.syncutil.AlbumSync.java
private void updateFolderTimeStamp(File localFolder) { long maxDate = 0; File[] files = localFolder.listFiles(new FilenameFilter() { public boolean accept(File current, String name) { File file = new File(current, name); return file.isFile() && !file.isHidden(); }// w w w. j a v a2 s . co m }); if (files != null) { for (File file : files) if (file.lastModified() > maxDate) maxDate = file.lastModified(); if (!localFolder.setLastModified(maxDate)) log.debug("Unable to set modification date for " + localFolder); } }
From source file:com.streamsets.pipeline.lib.remote.TestChrootSFTPClient.java
@Test public void testStat() throws Exception { File file = testFolder.newFile("file.txt"); final long modTime = 15000000000L; file.setLastModified(modTime); path = testFolder.getRoot().getAbsolutePath(); setupSSHD(path);/*w w w . jav a 2 s. c o m*/ SSHClient sshClient = createSSHClient(); for (ChrootSFTPClient sftpClient : getClientsWithEquivalentRoots(sshClient)) { // We can specify a file as either a relative path "file" or an absolute path "/file" and they should be // equivalent for (String p : new String[] { file.getName(), "/" + file.getName(), }) { Assert.assertEquals(modTime, sftpClient.stat(p).getMtime() * 1000); } } }
From source file:org.artifactory.repo.db.importexport.DbRepoExportHandler.java
private void exportFolder(FolderInfo sourceFolder) throws IOException { File targetDir = new File(settings.getBaseDir(), sourceFolder.getRelPath()); status.debug("Exporting directory '" + sourceFolder.getRepoPath() + "'...", log); // Insure that the source folder still exists. boolean sourceFolderExists = getFileService().exists(sourceFolder.getRepoPath()); if (!sourceFolderExists) { log.info("Skipping folder export : '{}', the source folder doesn't exists.", sourceFolder.getRepoPath()); return;/*w ww .j av a2 s .co m*/ } FileUtils.forceMkdir(targetDir); targetDir.setLastModified(sourceFolder.getLastModified()); if (settings.isIncludeMetadata()) { exportMetadata(targetDir, sourceFolder); } accumulator.accumulateSuccessfulFolder(); List<ItemInfo> children = getRepositoryService().getChildren(sourceFolder.getRepoPath()); for (ItemInfo child : children) { exportRecursive(child); } if (settings.isIncremental()) { cleanupIncrementalBackupDirectory(sourceFolder, children, targetDir); } }
From source file:com.edduarte.protbox.core.registry.PbxFile.java
/** * Sets the data of the file to the specified data. */// ww w . j a v a 2 s . co m public void writeSnapshotToFile(int index, File snapshotFile, FolderOption toFolder) throws ProtboxException { Snapshot snapshot; try { snapshot = snapshotStack.remove(index); snapshotStack.forcePush(snapshot); } catch (IndexOutOfBoundsException ex) { return; } try { if (toFolder.equals(FolderOption.SHARED)) { FileUtils.writeByteArrayToFile(snapshotFile, parentRegistry.encrypt(snapshot.getSnapshotData(), true)); } else { FileUtils.writeByteArrayToFile(snapshotFile, snapshot.getSnapshotData()); } snapshotFile.setLastModified(snapshot.getLastModifiedDate().getTime()); } catch (IOException ex) { throw new ProtboxException(ex); } }
From source file:nz.net.paulo.FileUploadHandler.java
@Override public void write(HttpServletResponse response) throws IOException { if (!rp.isMultiPart()) { throw new IllegalArgumentException( "Request is not multipart, please 'multipart/form-data' enctype for your form."); }/*from w ww . j a v a2s . c o m*/ response.setContentType("application/json"); JSONArray jsonArray = new JSONArray(); String fileName = extractFileName(rp.getContentDisposition()); Date lastModifiedDate = extractLastModifiedDate(rp.getContentDisposition()); try { // we write all the parts to the temp dir... for (Part part : rp.getParts()) { if (StringUtils.isBlank(fileName)) { String partContentDisposition = part.getHeader("content-disposition"); fileName = extractFileName(partContentDisposition); lastModifiedDate = extractLastModifiedDate(partContentDisposition); } String filePath = getFullFilePath(fileName); System.out.println("FilePath: " + filePath); part.write(filePath); if (isFilePart()) { File tempFile = new File(filePath); File partFile = rp.getPart(fileName); if (partFile.exists()) { if (partFile.lastModified() != lastModifiedDate.getTime()) { partFile.delete(); tempFile.delete(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "The file appears to have been modified since the last upload. Please try again."); return; } partFile = mergeFiles(partFile, tempFile); } else { tempFile.renameTo(partFile); } partFile.setLastModified(lastModifiedDate.getTime()); if (partFile.length() == getFileSize(rp.getContentRange())) { File finalFile = rp.getFileInUploadDir(fileName); partFile.renameTo(finalFile); } } File mainFile = rp.getFileInUploadDir(fileName); if (mainFile.exists()) { jsonArray.put(getFileJson(mainFile.getName(), mainFile.length())); } } String responseString = "{\"files\": " + jsonArray.toString() + "}"; try (PrintWriter writer = response.getWriter()) { writer.write(responseString); } } catch (IllegalStateException | ServletException e) { throw new IOException(e); } }
From source file:com.github.fritaly.dualcommander.Utils.java
private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate, List<String> exclusionList) throws IOException { // recurse// ww w .ja v a 2 s . c o m File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter); if (srcFiles == null) { // null if abstract pathname does not denote a directory, or if an I/O error occurs throw new IOException("Failed to list contents of " + srcDir); } if (destDir.exists()) { if (destDir.isDirectory() == false) { throw new IOException("Destination '" + destDir + "' exists but is not a directory"); } } else { if (!destDir.mkdirs() && !destDir.isDirectory()) { throw new IOException("Destination '" + destDir + "' directory cannot be created"); } } if (destDir.canWrite() == false) { throw new IOException("Destination '" + destDir + "' cannot be written to"); } for (File srcFile : srcFiles) { File dstFile = new File(destDir, srcFile.getName()); if (exclusionList == null || !exclusionList.contains(srcFile.getCanonicalPath())) { if (srcFile.isDirectory()) { doCopyDirectory(srcFile, dstFile, filter, preserveFileDate, exclusionList); } else { doCopyFile(srcFile, dstFile, preserveFileDate); } } } // Do this last, as the above has probably affected directory metadata if (preserveFileDate) { destDir.setLastModified(srcDir.lastModified()); } }
From source file:org.openflexo.toolbox.FileUtils.java
/** * An extension to Java's API rename method. Will attempt Java's method of doing the rename, if this fails, this method will then * attempt to forcibly copy the old file to the new file name, and then delete the old file. (This in appearance makes it look like a * file rename has occurred.) The method will also attempt to preserve the new file's modification times and permissions to equal that * of the original file's./*from w w w .jav a 2s .co m*/ * * @param source * File * @param destination * File * @return boolean * @throws IOException */ public static boolean rename(File source, File destination) throws IOException { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { // First (very important on Windows) delete the destination if it exists (rename will fail on Windows if destination // exists) if (destination.exists()) { destination.delete(); } // Do a normal API rename attempt if (source.renameTo(destination)) { return true; } FileUtils.createNewFile(destination); // API rename attempt failed, forcibly copy bis = new BufferedInputStream(new FileInputStream(source)); bos = new BufferedOutputStream(new FileOutputStream(destination)); // Do the copy pipeStreams(bos, bis); // Close the files bos.flush(); // Close the files bis.close(); bos.close(); // Attempt to preserve file modification times destination.setLastModified(source.lastModified()); if (!source.canWrite()) { destination.setReadOnly(); } // Delete the original source.delete(); bis = null; bos = null; return true; } finally { try { if (bis != null) { bis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:jenkins.plugins.ivyreport.IvyAccess.java
/** * Will only copy the file from the repository if its last modified time * exceeds what the instance thinks is the last recorded modified time of * the localFile, which is the local backup ivy file copy. For this to * operate properly for remoting circumstances, the master and slave * instances must be reasonably time synchronized. * // w ww. ja va 2s . com * @param source * Workspace root Directory * @param target * The local file to be copied to * @return true iff the file was actually copied * @throws IOException * If unable to access/copy the workspace ivy file * @throws InterruptedException * If interrupted while accessing the workspace ivy file */ private boolean copyIvyFileFromWorkspaceIfNecessary(FilePath source, File target) throws IOException, InterruptedException { boolean copied = false; if (source != null) { // Unless the workspace is non-null we can not // copy a new ivy file // Copy the ivy file from the workspace (possibly at a slave) to the // projects dir (at Master) FilePath backupCopy = new FilePath(target); long flastModified = source.lastModified(); if (flastModified == 0l) throw new FileNotFoundException("Can't stat file " + source); if (flastModified > backupCopy.lastModified()) { source.copyTo(backupCopy); target.setLastModified(flastModified); copied = true; LOGGER.info("Copied the workspace ivy file to backup"); } } return copied; }