• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

How can i check File is Transfered/Copied completely in a folder,through a java code.

My Friend is copying a large File in a specified folder.How can i check through java code that file transfer is completed.
0
manzar_chaudhary
Asked:
manzar_chaudhary
  • 8
  • 6
1 Solution
 
CEHJCommented:
Check the file size? ME5 sum?
0
 
manzar_chaudharyAuthor Commented:
file.length() remains constant during the course of transfer.
0
 
Gibu GeorgeChief Technology OfficerCommented:
You need to check the size of the file in the target folder  and then compare with source folder
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
manzar_chaudharyAuthor Commented:
i don't have acces to the source folder.further if i check the filesize (file.length()) it is constant through out the transfer.
0
 
CEHJCommented:
You can't tell if it's complete with certainty unless you know the source file size. When/if you do, you can compare the size as i mentioned.
0
 
CEHJCommented:
>>i don't have acces to the source folder.

How do/can you transfer it then?
0
 
manzar_chaudharyAuthor Commented:
As i said my friend is copying the file manually.I have a thread which monitors the specified folder.
0
 
CEHJCommented:
Actually you didn't mention anything about manually. There is no way to know unless you somehow store the file's size in the file itself
0
 
manzar_chaudharyAuthor Commented:
I have written following snippet and it worked but i don't know whether its a proper way to do it.
public static void main(String args[]){
        boolean flag = true;
        while(flag){
            File file  = new File("XXX.xxx");
            if(file.exists()){
                BufferedReader br;
                try {
                    Thread.sleep(1000);
                    br = new BufferedReader(new FileReader(file));
                    while(br.readLine() != null){
                        
                    }
                    System.err.println("File Copied..");
                    flag = false;
                } catch (Exception ex) {
                    System.err.println("File is Getting Copied..");
                    
                }
            }
        }
    }

Open in new window

0
 
CEHJCommented:
You might try obtaining a lock on the File. It shouldn't let you if it's being written to
0
 
manzar_chaudharyAuthor Commented:
yes you r write..but is there any harm in acquiring a lock..
actually windows(dnt know bout linux) locks the file while copying it and dosent release the lock  untill an unless it copies the file completely..i am just exploiting it..
thanks for suggesting a better way of accquiring a lock
0
 
CEHJCommented:
>>yes you r write..but is there any harm in acquiring a lock..

None. As soon as you get it you can release it in a finally block
0
 
manzar_chaudharyAuthor Commented:
so that means i can figure out whether file is getting tranfer or trasnferd completely using lock method.
do you think is there any other mechanism to do so...
0
 
CEHJCommented:
I think that's the simplest way, but i wouldn't like to say any is infallible unless it's the way i last mentioned. You should experiment
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now