Link to home
Start Free TrialLog in
Avatar of paluindian
paluindian

asked on

Remote machine file access.

Hello All,

In my automation, I need to compare files residing on remote machines with the files on local machine. Does anybody have idea how can I do that? I have java file comaprison program which will compare 2 files.

Java compare file1 file2

How can I specify the file2 which resides on remote machine? Can I do this with FTP to the desired folder and copy it to the local folder and compare? how can I automate this process?

Thanks,
paluindian
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Depends on what file serving processes are running on the remote machine. FTP is a possibility if it's running. If the remote machine's on your LAN then you can probably access it directly if the files are shared
Avatar of bvanderveen
bvanderveen

If the file system is shared, you may be able to look at them directly.  Otherwise, I would suggest something like

RMI process to send file over the wire to other machine.
Compare line-by-line (or byte by byte if binary).
> Compare line-by-line (or byte by byte if binary).

I'd compare byte by byte regardless of the file type.

As far as accessing the file on the remote server you can use whatever is availablke to you.
You shouldn't have to copy the remote file locally, and instead just compare the file bytes as you read them from the remote location.
>>
> Compare line-by-line (or byte by byte if binary).

I'd compare byte by byte regardless of the file type.
>>

No - you were right in the first place


You can greatly increase performance and reliability if you store the remote file's checksum remotely and instead compare that to the checksum of the local file
>>No - you were right in the first place

That is, if FTP's occurring, which you hint at as a possibility
> That is, if FTP's occurring, which you hint at as a possibility

ftp makes no difference
you should still compare byte by byte.
It depends on how paluindian wants to approach it. Text files may be effectively identical but due to newline differences they may compare as different
If you aren't comparing checksums (and if you want to list out where the differences are, that won't help much), you may still want a line-by-line comparison.

If you use a readline() method for text files, you can ignore the newline differences, because they are stripped out.  And, that may fit with what he wants to do.  If he is comparing, say, a properties file, a listing showing the last byte on each line is dfferent, would be useless.
>>If he is comparing ... would be useless.

That's precisely my point ;-)
Avatar of paluindian

ASKER

Hello Everybody,

Thanks for the responses...
I already have written program for file/dir comparison and it is using byte comparison. I would like to know how can I access that remote file to send to  my filecomparison program?
Are you suggesting me RMI ? can you give me sample program? I'm little confused...please help me.

Thanks,
Paluindian
> I would like to know how can I access that remote file to send to  my filecomparison program?

it depends whats availoable.
you need something on the server side to access the file
eg. ftp, a cgi, a servlet ....

(rmi would seem a bit of an overkill)
Yes on remote machine ( AIX,SOLARIS,Z/OS ) FTP is available.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Get your stream using:

InputStream in = new URL("ftp://username:password@domain.com/file.ext").openStream();

And pass that to your existing comparison code.

Wow...that worked!!!
Thanks everybody and especially CEHJ and objects...
Paluindian
glad i could help, look me up if you need further assistance in the future.

(don't use that accepted code though, its buggy :)
all you need is the code I posted.
8-)
Hello CEHJ and Objects,

Actually as I told earlier,filecomparison program is already written and it is used as a library. And it accepts two arguments as file...

java filecompare file1 file2

Now file1 : local file/validated file
       file2:  "ftp://username:password@domain.com/file.ext")

Now our program does not accept input as inputstream...I need to pass it as File argument.
I tried few ways...but it did not work. I can not change pre-written filecomparison code. Any workaround?

Thanks in advance...
Paluindian
> Actually as I told earlier,filecomparison program is already written and it is used as a library.

yes I realised that, thats why I only posted how to get a stream as it is all you need.

>  I can not change pre-written filecomparison code. Any workaround?

Then you'll need to first save the file to disk.
Hey objects...
Thanks so much...I guess I will have to do that...saving file on the disk.

Thanks once again,
Paluindian
>>I can not change pre-written filecomparison code. Any workaround?

You might try extending the filecomparison code.  The io classes are all pretty much alike, so you might be able to change only 1 or 2 methods, and use super() methods for the rest...
Hi bvanderveen,

Yes, actually my team lead liked this idea and she is going to extend the super class to fit this functionality.

Thanks,
Paluindian
You should normally make your chief method have parameters of type Out/InputStream when working in this area, as it's the most versatile
Hello All,
The good news is Team lead told me that I can user method overloading.
So now I need method which will overload the followin method: as you suggested in the signature

public static long fileCompare(InputStream in1, InputStream in2)

How can I decide my success here? and return long? In the following code it is returning length of the file.

public static long fileCompare(File file1, File file2) {
      
      
      long file1Length = file1.length();
      long file2Length = file2.length();

      try {
            BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(file1));
            BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file2));

            while ((bis1.read() == bis2.read())) {
                  if ( file1Length <= 0L)
                  break;
                  file1Length--;
            }

            if (file1Length != 0L) {
                  System.err.println("ERROR: Files: " + file1 + " and " + file2 + " differ at byte " + (file1.length() - file1Length + 1));
            } else {
                  System.err.println("Files :\n" + file1 + "\n" + file2 + "\nCompared Successfully \n");
            }

            bis1.close();
            bis2.close();
            
            ));
                return (long)(file1Length);
            } catch(IOException e) {
                  e.printStackTrace();
            }
        return (long)(file1Length);
      } // End Of Method fileCompare

      public static int showHelpAndExit() {
            System.err.println("Usage: java diff file1 file2");
            return 4;
      }



Thanks
Paluindian
method to take two stream would look like:

public static long fileCompare(InputStream in1, InputStream in2) {
     long file1Length = file1.length();
     long file2Length = file2.length();

     try {
          BufferedInputStream bis1 = new BufferedInputStream(in1);
          BufferedInputStream bis2 = new BufferedInputStream(in2);

          while ((bis1.read() == bis2.read())) {
               if ( file1Length <= 0L)
               break;
               file1Length--;
          }

          if (file1Length != 0L) {
               System.err.println("ERROR: Files: " + file1 + " and " + file2 + " differ at byte " + (file1.length() - file1Length + 1));
          } else {
               System.err.println("Files :\n" + file1 + "\n" + file2 + "\nCompared Successfully \n");
          }

          bis1.close();
          bis2.close();
         
          ));
                return (long)(file1Length);
          } catch(IOException e) {
               e.printStackTrace();
          }
        return (long)(file1Length);
     } // End Of Method fileCompare
How would you take parameters file1 and file2 for following?


long file1Length = file1.length();
long file2Length = file2.length();

if you meant in1 and in2 instead of file1/file2 then lenght is not method for inputstream right? Or am I missing something?

Thanks
paluindian


woops didn't see that.

what you would do is instead read until eof.
I dont want to bug you...that thing striked me too but then how would I return long int as previous method return?
Is it a good idea to loop through the both inputsteam and get a counter till EOF and then while comparing bytes decrease the counter by 1 during each iteration...

What I meant is following

while (bis1.read() <= -1) {
bis1lenghth++;
}
while(bis2.read() <= -1)  {
bis2length++:
}

if ( bis1length == bis2length) {
while ((bis1.read() == bis2.read())) {
               if ( bs1length <= 0L)
               break;
               bis1length--;
          }
}


if (bis1length != 0L) {
               System.err.println("ERROR: Files: " + file1 + " and " + file2 + " differ at byte " + (file1.length() - file1Length + 1));
          } else {
               System.err.println("Files :\n" + file1 + "\n" + file2 + "\nCompared Successfully \n");
          }

return bis1length;
}


is this a good idea??
Paluindian



your reading the streams twice, you only need to read it once.
and yes count the bytes read as you go.

When I pass argument in the form:

InputStream in = new URL("ftp://username:password@domain.com/file.ext").openStream();

I get exception :

java.net.MalformedURLException: no protocol:

What does that mean?
Paluindian
thought you said earlier that that worked??
No...that time I tried it manually. By putting that URL in windows Explorer.
Now when I actually run that URL in java program I was getting above exception. That I removed now but I get FileNotFoundException.

java.io.FileNotFoundException:/opt/home/testdir/test.txt :  A file or directory in the path name does not exist.

File permissions on test.txt are  -rw-r--r--

and I am connecting as authorized user not anonymous user.

Thanks,
Paluindian

you shouldn't have accepted that comment as an answer then if it didn't work :)
Unfortunately some experts here will not be as eager to help you once they have there hands on your points.
You can ask CS to reopen the question if you like.
I guess it is worth putting another question to give credit to efforts everyody has takes...thanks objects...I'm opening new topic :-)

See ya there.
Paluindia
>>you shouldn't have accepted that comment as an answer then if it didn't work :)

ROTFL - and i suppose your 'answer' which reproduced the central principle of the one i'd already posted, which rightly did not attract any points, should be similarly discounted should it?

paluindian - there's nothing wrong with that principle - where are you at now?