Results 1 to 4 of 4

Thread: Check whether two Filename paths refer to the same file or not

  1. #1
    Join Date
    Nov 2009
    Posts
    680

    Post Check whether two Filename paths refer to the same file or not

    Hi, can anyone help me? I am in trouble. I am making my project in java, and got one problem. I am not able to code which will Check whether two Filename paths refer to the same file or not? Can anyone has an idea which will help me. Please, give me solution. I am waiting for your reply. You can give me any logic, so I can use it for my purpose.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Check whether two Filename paths refer to the same file or not

    Hi, the following program will check whether the file name refers to the same file or not. The program is making use of the methods such as
    equals() to check whether the pathname are equal or not and getCanonicalFile() to determine the same file or directory its abstract pathname or not.

    import java.io.*;
    public class SameFile
    {
    public static void main(String args[])
    {

    File file1 = new File("filename1.txt");
    File file2 = new File("filename.txt");
    System.out.println("Actual fileName1 = " + file1 );
    System.out.println("Actual fileName2 = " + file2 );
    boolean b = file1.equals(file2);
    System.out.println("It checks whether the file name paths are equal or not" + b);
    try
    {
    file1 = file1.getCanonicalFile();
    file2 = file2.getCanonicalFile();
    System.out.println("Actual path of filName1 = " + file1);
    System.out.println("Actual path of fileName2 = " + file2);
    }
    catch (IOException e)
    {
    System.out.println("IOException is "+ e);
    }
    b = file1.equals(file2);
    System.out.println("The filenames are now equl" + b);
    System.out.println("Actual path of fileName1 = " + file1 );
    System.out.println("Actual path of fileName2 = " + file2);
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Check whether two Filename paths refer to the same file or not

    The program below demonstrates how to determine if two filename paths refer to the same file or not.

    public class FilePathChecking
    {
    private static void doTest()
    {
    File fname1 = new File("./InputFile.txt");
    File fname2 = new File("InputFile.txt");
    System.out.println();
    System.out.println("Original Filename of file 1 = " + fname1);
    System.out.println("Original Filename of file 2 = " + fname2);
    boolean b = fname1.equals(fname2);
    System.out.println("Check to determine if the two filename paths are equal: (" +b + ")\n");
    try
    {
    fname1 = fname1.getCanonicalFile();
    fname2 = fname2.getCanonicalFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println("Normalized Path for Filename1 = " + fname1 );
    System.out.println("Normalized Path for Filename2 = " + fname2 );
    b = fname1.equals(fname2);
    System.out.println( "Check to determine if the two filename paths are equal: (" + b + ")\n");
    }
    public static void main(String[] args)
    {
    doTest();
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Check whether two Filename paths refer to the same file or not

    Hi, you want to determine If Two Filename Paths Refer to the Same File or not and here I have got the code on internet. Just make use of the logic of it. It will help you a lot.

    Code:
        File file1 = new File("./filename");
        File file2 = new File("filename");
        boolean b = file1.equals(file2);    
        try {
            file1 = file1.getCanonicalFile(); 
            file2 = file2.getCanonicalFile(); 
        } 
        catch (IOException e) 
        {
        }
        b = file1.equals(file2);

Similar Threads

  1. What does memory.dmp file refer to
    By cupid! in forum Windows XP Support
    Replies: 3
    Last Post: 23-03-2012, 10:52 AM
  2. Batch file to check directory
    By Trini Alvarado in forum Software Development
    Replies: 5
    Last Post: 01-04-2010, 11:00 AM
  3. How do you check on Vista if .exe file is 32-bit or 64-bit?
    By gee9782@yahoo.com in forum Vista Help
    Replies: 5
    Last Post: 04-09-2009, 08:43 PM
  4. How to check with vb.net whether excel file is open
    By Guns-n-Roses in forum Software Development
    Replies: 3
    Last Post: 10-08-2009, 12:56 PM
  5. File in Use - filename.xls is locked for editing by ' '.
    By Barkley Bees in forum Windows Server Help
    Replies: 1
    Last Post: 19-08-2008, 10:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,714,120.82797 seconds with 17 queries