Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remote machine file access.

Posted on 2004-08-25
36
Medium Priority
?
125 Views
Last Modified: 2012-06-08
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
0
Comment
Question by:paluindian
  • 12
  • 11
  • 10
  • +1
36 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897428
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
0
 
LVL 7

Expert Comment

by:bvanderveen
ID: 11897795
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).
0
 
LVL 92

Expert Comment

by:objects
ID: 11897915
> 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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 11897953
>>
> 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


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897966
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897973
>>No - you were right in the first place

That is, if FTP's occurring, which you hint at as a possibility
0
 
LVL 92

Expert Comment

by:objects
ID: 11897999
> That is, if FTP's occurring, which you hint at as a possibility

ftp makes no difference
you should still compare byte by byte.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11898017
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
0
 
LVL 7

Expert Comment

by:bvanderveen
ID: 11898057
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11898074
>>If he is comparing ... would be useless.

That's precisely my point ;-)
0
 

Author Comment

by:paluindian
ID: 11898199
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
0
 
LVL 92

Expert Comment

by:objects
ID: 11898222
> 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)
0
 

Author Comment

by:paluindian
ID: 11898263
Yes on remote machine ( AIX,SOLARIS,Z/OS ) FTP is available.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11898332
You can do something like

final int BUF_SIZE = 1 << 10;
byte[] bufferRemote = new byte[BUF_SIZE];
byte[] bufferLocal = new byte[BUF_SIZE];
InputStream remoteIn = new URL("ftp://username:password@somewhere.com/somefile.txt").openStream();
InputStream localIn = new FileInputStream("somefile.txt");
buffersEqual = true;
while((remoteIn.read(bufferRemote) > -1 && buffersEqual) {
      localIn.read(bufferLocal);
      buffersEqual = Arrays.equal(bufferRemote, bufferLocal);
}
System.out.println("Files are equal? " + buffersEqual);
// close everything
0
 
LVL 92

Expert Comment

by:objects
ID: 11898345
Get your stream using:

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

And pass that to your existing comparison code.

0
 

Author Comment

by:paluindian
ID: 11898424
Wow...that worked!!!
Thanks everybody and especially CEHJ and objects...
Paluindian
0
 
LVL 92

Expert Comment

by:objects
ID: 11898446
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11898450
8-)
0
 

Author Comment

by:paluindian
ID: 11899747
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
0
 
LVL 92

Expert Comment

by:objects
ID: 11899782
> 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.
0
 

Author Comment

by:paluindian
ID: 11899870
Hey objects...
Thanks so much...I guess I will have to do that...saving file on the disk.

Thanks once again,
Paluindian
0
 
LVL 7

Expert Comment

by:bvanderveen
ID: 11902242
>>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...
0
 

Author Comment

by:paluindian
ID: 11906164
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11906211
You should normally make your chief method have parameters of type Out/InputStream when working in this area, as it's the most versatile
0
 

Author Comment

by:paluindian
ID: 11907445
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
0
 
LVL 92

Expert Comment

by:objects
ID: 11907529
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
0
 

Author Comment

by:paluindian
ID: 11908011
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


0
 
LVL 92

Expert Comment

by:objects
ID: 11908037
woops didn't see that.

what you would do is instead read until eof.
0
 

Author Comment

by:paluindian
ID: 11908103
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



0
 
LVL 92

Expert Comment

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

0
 

Author Comment

by:paluindian
ID: 11908238
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
0
 
LVL 92

Expert Comment

by:objects
ID: 11908398
thought you said earlier that that worked??
0
 

Author Comment

by:paluindian
ID: 11908705
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

0
 
LVL 92

Expert Comment

by:objects
ID: 11908738
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.
0
 

Author Comment

by:paluindian
ID: 11908766
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11911002
>>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?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question