Compare two files by content in Java
In this tutorial, we will provide a simplified approach to compare two files in Java by content. We will use Apache Commons IO library to perform this comparison, and run this against some test files we prepare to see the results.
Before start download commons-io-2.4.jar and add in your classpath.
We will use the contentEquals() method of the FileUtils class of the above library to compare the file content.
The Java program to compare two files by content using Apache Commons IO, is provided below:
package com.simplecode.jdbc; import java.io.File; import org.apache.commons.io.FileUtils; public class compareFileContent { public static void main(String[] args) throws Exception { /* Get the files to be compared first */ File file1 = new File(args[0]); File file2 = new File(args[1]); boolean compareResult = FileUtils.contentEquals(file1, file2); System.out.println("Are the files are same? " + compareResult); } }
Recommended Article
The comparison works because, it is done as a byte by byte comparison between the two files as per the API docs. You can compare any two files using this code, be it of any format. One drawback of this approach, is that if one of the files has additional metadata but same content, the comparison still returns false. This is acceptable in one angle and otherwise. So, you may want to make use of this approach depending on your requirement.
How To Change The File Last Modified Date In Java?
Hereâs an example to change the fileâs last modified date with the help of File.setLastModified() . This method accept the new modified date in milliseconds (long type), So some data type conversion are required.
import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ChangeFileLastModifiedDate { public static void main(String[] args) { try { File file = new File("C:\File.pdf"); // Print the original last modified date SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); System.out.println("Original Last Modified Date : " + sdf.format(file.lastModified())); // Set this date String newFileDate = "11/17/2000"; // Need convert the above date to milliseconds in long value Date newDate = sdf.parse(newFileDate); file.setLastModified(newDate.getTime()); // Print the latest last modified date System.out.println("Lastest Last Modified Date : " + sdf.format(file.lastModified())); } catch (ParseException e) { e.printStackTrace(); } } }
Result :
Original Last Modified Date : 12/20/2011
Lastest Last Modified Date : 11/17/2000
How To Get The File’s Last Modified Date & time In Java?
In Java, you can use the File.lastModified() to get the file’s last modified timestamps. This method will returns the time in milliseconds (long value), you may to format it with SimpleDateFormat to make it a human readable format.
File Last Modified Date and Time
import java.io.File; import java.text.SimpleDateFormat; public class GetFileLastModifiedDate { public static void main(String[] args) { File file = new File("c:\Core java.ppt"); System.out.println("Before Format : " + file.lastModified()); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); System.out.println("After Format : " + sdf.format(file.lastModified())); } }
Result :
Before Format : 1359018283231
After Format : 03/24/2013 12:34:33
How to find or check hidden files in Java ?
we can check the file property is hidden or not and accordingly we can use that file in our application. isHidden() method is provided on file Class in Java which we will use to test this property.
Syntax of the method:
public static boolean isHidden(Path path) throws IOException
Here we pass the path of the file for which we want to test hidden property and it will return true if it’s a hidden file otherwise will get false value.
Here is complete code example of finding hidden file in Java, we are using file.isHidden() method to check whether a file is hidden or not.
import java.io.File; public class FileHiddenExample{ public static void main(String[] args)throws SecurityException ,IOException{ File file = new File("C:/HiddenTest.txt"); if (file.isHidden()) { System.out.println("This file is Hidden file: "); }else { System.out.println("File is not Hidden "); } } }
The isHidden() method is system dependent, on UNIX platform, a file is considered hidden if it’s name is begins with a dot symbol (.); On Microsoft Windows platform, a file is considered to be hidden, if it’s marked as hidden in the file properties.
How to delete file if exists in java?
package com.simplecode.com; import java.io.File; public class FileDemo { public static void main(String[] args) { try{ String tempFile = "C:/mydir/myfile.txt"; //Delete if tempFile exists File fileTemp = new File(tempFile); if (fileTemp.exists()){ fileTemp.delete(); } }catch(Exception e){ // if any error occurs e.printStackTrace(); } } }Read More
How To Delete File In Java
The command File.delete() to delete a file in java, it will return a boolean value to indicate the delete operation status;true
if the file is deleted;false
if failed.
Example
In this example, it will delete a log file named “C:\\sample.txt”
package com.simplecode.file; import java.io.File; public class DeleteFile { public static void main(String[] args) { try{ File file = new File("c:\sample.txt"); if(file.delete()){ System.out.println(file.getName() + " is deleted!"); }else{ System.out.println("Delete operation is failed."); } }catch(Exception e){ e.printStackTrace(); } } }Read More