Simulate touch command with Java - java

I want to change modification timestamp of a binary file. What is the best way for doing this?
Would opening and closing the file be a good option? (I require a solution where the modification of the timestamp will be changed on every platform and JVM).

The File class has a setLastModified method. That is what ANT does.

My 2 cents, based on #Joe.M answer
public static void touch(File file) throws IOException{
long timestamp = System.currentTimeMillis();
touch(file, timestamp);
}
public static void touch(File file, long timestamp) throws IOException{
if (!file.exists()) {
new FileOutputStream(file).close();
}
file.setLastModified(timestamp);
}

Since File is a bad abstraction, it is better to use Files and Path:
public static void touch(final Path path) throws IOException {
Objects.requireNonNull(path, "path is null");
if (Files.exists(path)) {
Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
} else {
Files.createFile(path);
}
}

Here's a simple snippet:
void touch(File file, long timestamp)
{
try
{
if (!file.exists())
new FileOutputStream(file).close();
file.setLastModified(timestamp);
}
catch (IOException e)
{
}
}

I know Apache Ant has a Task which does just that.
See the source code of Touch (which can show you how they do it)
They use FILE_UTILS.setFileLastModified(file, modTime);, which uses ResourceUtils.setLastModified(new FileResource(file), time);, which uses a org.apache.tools.ant.types.resources.Touchable, implemented by org.apache.tools.ant.types.resources.FileResource...
Basically, it is a call to File.setLastModified(modTime).

This question only mentions updating the timestamp, but I thought I'd put this in here anyways. I was looking for touch like in Unix which will also create a file if it doesn't exist.
For anyone using Apache Commons, there's FileUtils.touch(File file) that does just that.
Here's the source from (inlined openInputStream(File f)):
public static void touch(final File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
final File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
final OutputStream out = new FileOutputStream(file);
IOUtils.closeQuietly(out);
}
final boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}

If you are already using Guava:
com.google.common.io.Files.touch(file)

Related

Monitoring Zip4J extractAll() method progress monitor

I am using Zip4J for extracting zip file and I am able to do it. However, I want to use progress monitor provided in Zip4J but not able to use it successfully.
The documentation only says that it should have run in thread mode true. I did it and my console stuck on this on command line. Any working example of extractAll() with progress monitor.
public String unzipFile(String sourceFilePath, String extractionPath) {
String extractionDirectory = "";
FileHeader fileHeader = null;
if (FileUtility.isPathExist(sourceFilePath) && FileUtility.isPathExist(extractionPath)) {
try {
ZipFile zipFile = new ZipFile(sourceFilePath);
LOG.info("File Extraction started");
List<FileHeader> fileHeaderList = zipFile.getFileHeaders();
if (fileHeaderList.size() > 0)
fileHeader = (FileHeader) fileHeaderList.get(0);
if (fileHeader != null)
extractionDirectory = splitFileName(fileHeader.getFileName());
long totalPercentage = 235;
long startTime = System.currentTimeMillis();
zipFile.extractAll(extractionPath);
LOG.info("File Extraction completed.");
System.out.println();
} catch (ZipException e) {
LOG.error("Extraction Exception ->\n" + e.getMessage());
}
} else {
LOG.error("Either source path or extraction path is not exist.");
}
return extractionDirectory;
}
Don't know, works fine if you add enough files, that there actually is a progress to see. I added some really fat ones for the purpose.
#Test
public void testExtractAllDeflateAndNoEncryptionExtractsSuccessfully() throws IOException {
ZipFile zipFile = new ZipFile(generatedZipFile);
List<File> toAdd = Arrays.asList(
getTestFileFromResources("sample_text1.txt"),
getTestFileFromResources("sample_text_large.txt"),
getTestFileFromResources("OrccTutorial.pdf"),
getTestFileFromResources("introduction-to-automata-theory.pdf"),
getTestFileFromResources("thomas.pdf")
);
zipFile.addFiles(toAdd);
zipFile.setRunInThread(true);
zipFile.extractAll(outputFolder.getPath());
ProgressMonitor mon = zipFile.getProgressMonitor();
while (mon.getState() == BUSY) {
System.out.println(zipFile.getProgressMonitor().getPercentDone());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
ZipFileVerifier.verifyFolderContentsSameAsSourceFiles(outputFolder);
verifyNumberOfFilesInOutputFolder(outputFolder, 5);
}
testAddFilesWithProgressMonitor.java in the the project's test cases shows how to use ProgressMonitor.

How to check if file was downloaded completely via Spring Rest Api

I have created simple rest api to serve files from hdfs (Files are large and I don't want to copy them locally).
I would like to log information that file download completed successfully i.e. whole stream was read, but I do not know how. I can only log information that file download was started.
I will appreciate any help.
#Autowired
private FileDownloadService fds;
#RequestMapping(value = GET_FILE_PATH, method = RequestMethod.GET)
#Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity getFileStream(#RequestParam("name") String name) {
LOG.info("Processing for filename: " + name);
try {
Path p = fds.getFilePath(name);
org.apache.hadoop.fs.FSDataInputStream is = fds.getFileStream(p);
return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"" + p.getName() + "\"'").contentLength(fds.getFileLength(p))
.contentType(MediaType.APPLICATION_OCTET_STREAM).body(new InputStreamResource(is));
} catch (Exception e) {
LOG.error(e.getLocalizedMessage(), e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error");
} finally {
LOG.info("File: " + name + " download started");
}
}
You can try to create a wrapper over InputStream and trigger some flag on stream closing (close()).
For instance you can take ProxyInputStream as a basis:
ProxyInputStreamis = new ProxyInputStream(fds.getFileStream(p)) {
#Override
public void close() throws IOException {
super.close();
// some trigger
}
};

Removing all files but those with video extension

I'm trying to delete every file from a directory (and it's subdirectories, very important) which isn't a video file.Here is my ProcessDirectory class:
public class ProcessDirectory {
private File directory;
public static final String [] MOVIE_EXTENSIONS = {"avi", "mp4", "flv", "mkv"};
public ProcessDirectory(String path) {
this.directory = new File(path);
}
private Collection<File> findMovieFiles() throws IOException {
System.out.println("Getting all .avi, .mp4 files in " + this.directory.getCanonicalPath()
+ " including those in subdirectories");
Collection<File> videoFiles = FileUtils.listFiles(this.directory, MOVIE_EXTENSIONS, true);
return videoFiles;
}
public void removeAllNonMovieFiles() throws IOException {
Collection<File> movieFiles = findMovieFiles();
Collection<File> allFilesAndFolders = FileUtils.listFilesAndDirs(this.directory, TrueFileFilter.TRUE, TrueFileFilter.TRUE);
// have to use Iterator because otherwise it throws ConcurrentModificationException
Iterator<File> iter = allFilesAndFolders.iterator();
while (iter.hasNext()) {
File currentElement = iter.next();
if (!movieFiles.contains(currentElement)) {
iter.remove();
}
}
}
}
And here is where I call the method:
String path = "/run/media/michal/F04AA6E24AA6A536/Filmy/FilmyTest/";
ProcessDirectory directory = new ProcessDirectory(path);
try {
directory.removeAllNonMovieFiles();
} catch (IOException e) {
e.printStackTrace();
}
It doesn't seem to work - no files are deleted. Both of my Collections are fine - checked them all with System.out and they have correct files in them, but they arent removing anything.
EDIT: Changed my code, I think it looks better now now but still doesn't work.
iter.remove() just removes the file from the collection. You are missing the part where the files are actually deleted, like Files.delete or something like that.
Do you know java.nio.file.Files? With it you can even write code like the following:
Files.walk(Paths.get("your path here"))
.filter(isNotAMovieFile())
.forEach(delete());
This way you only need to implement Predicate<Path> isNotAMovieFile() and Consumer<Path> delete() which should be pretty much straight-forward.

Java file deletion fails

I need to delete files from within a java program and have written this code. It fails to delete the file and I can't figure why. The File is not in use and not write protected.
public static void delfile(String filetodel) {
try {
File file = new File("filetodel");
if (file.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed." + filetodel);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I guess the issue is this:
File file = new File("filetodel");
This should possibly be (inferred from the parameter filetodel passed in the method):
File file = new File(filetodel);
Everything else seems fine, and is working on my machine.
If you just want to delete the file, there is no need for loading it.
java.nio.file.Files.deleteIfExists(filetodel); (where filetodel contains the path to the file)
Returns true if the file was deleted, so you can even put it in your if-clause.
hey buddy you should use a path as parameter in delete
static void delete(Path path)
Deletes a file.
static boolean deleteIfExists(Path path)
Deletes a file if it exists.
search here: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
so in your case
File file = new File("c://user//filetodel");
file.delete();
or use getAbsolutePath(filename) and use it in file path
Here is my code to delete file.
public class deletef
{
public static void main(String[] args)
{
try{
File file = new File("/home/rahul/Downloads/ou.txt");
if(file.delete()){
System.out.println(file.getName() + " is deleted!");
}else{
System.out.println("Delete operation is failed.");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
your code is also right but you have to put extension also in your file
File file = new File("filetodel");
here add extension also of file other wise your code will not delete file

How to display the contents of a directory

I need to write a recursive algorithm to display the contents of a directory in a computer's file system but I am very new to Java. Does anyone have any code or a good tutorial on how to access a directory in a file system with Java??
You can use the JFileChooser class, check this example.
Optionally you can also execute native commands like DIR , lsusing java , here is an example
This took me way too long to write and test, but here's something that should work.
Note: You can pass in either a string or file.
Note 2: This is a naive implementation. Not only is it single-threaded, but it does not check to see if files are links, and could get stuck in an endless loop due to this.
Note 3: The lines immediately after comments can be replaced with your own implementation.
import java.io.*;
public class DirectoryRecurser {
public static void parseFile(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (file.exists()) {
parseFile(file);
} else {
throw new FileNotFoundException(file.getPath());
}
}
public static void parseFile(File file) throws FileNotFoundException {
if (file.isDirectory()) {
for(File child : file.listFiles()) {
parseFile(child);
}
} else if (file.exists()) {
// Process file here
System.out.println(file.getPath());
} else {
throw new FileNotFoundException(file.getPath());
}
}
}
Which could then be called something like this (using a Windows path, because this Workstation is using Windows):
public static void main(String[] args) {
try {
DirectoryRecurser.parseFile("D:\\raisin");
} catch (FileNotFoundException e) {
// Error handling here
System.out.println("File not found: " + e.getMessage());
}
}
In my case, this prints out:
File not found: D:\raisin
because said directory is just one I made up. Otherwise, it prints out the path to each file.
Check out Apache Commons VFS: http://commons.apache.org/vfs/
Sample:
// Locate the Jar file
FileSystemManager fsManager = VFS.getManager();
FileObject jarFile = fsManager.resolveFile( "jar:lib/aJarFile.jar" );
// List the children of the Jar file
FileObject[] children = jarFile.getChildren();
System.out.println( "Children of " + jarFile.getName().getURI() );
for ( int i = 0; i < children.length; i++ )
{
System.out.println( children[ i ].getName().getBaseName() );
}
If you need to access files on a network drive, check out JCIFS: http://jcifs.samba.org/
check this out buddy
http://java2s.com/Code/Java/File-Input-Output/Traversingallfilesanddirectoriesunderdir.htm
public class Main {
public static void main(String[] argv) throws Exception {
}
public static void visitAllDirsAndFiles(File dir) {
System.out.println(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}
}
For each file you need to check if it is a directory. If it is, you need to recurse. Here is some untested code, which should help:
public void listFiles(File f){
System.out.println(f.getAbsolutePath());
if(f.isDirectory()){
for (File i : f.listFiles()){
listFiles(i);
}
}
}

Categories