Solved

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

Posted on 2009-05-18
15
327 Views
Last Modified: 2013-11-23
My Friend is copying a large File in a specified folder.How can i check through java code that file transfer is completed.
0
Comment
Question by:manzar_chaudhary
  • 8
  • 6
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24412151
Check the file size? ME5 sum?
0
 

Author Comment

by:manzar_chaudhary
ID: 24412198
file.length() remains constant during the course of transfer.
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24412270
You need to check the size of the file in the target folder  and then compare with source folder
0
 

Author Comment

by:manzar_chaudhary
ID: 24412294
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
 
LVL 86

Expert Comment

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

Expert Comment

by:CEHJ
ID: 24412326
>>i don't have acces to the source folder.

How do/can you transfer it then?
0
 

Author Comment

by:manzar_chaudhary
ID: 24412455
As i said my friend is copying the file manually.I have a thread which monitors the specified folder.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

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

Author Comment

by:manzar_chaudhary
ID: 24412693
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24412730
You might try obtaining a lock on the File. It shouldn't let you if it's being written to
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24412741
0
 

Author Comment

by:manzar_chaudhary
ID: 24412797
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
 
LVL 86

Expert Comment

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

Author Comment

by:manzar_chaudhary
ID: 24412844
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 24412881
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 56
Java SE 8u111  Lot of stuff broke 11 54
Arrays.asList  VS  ArrayList 4 45
factorial example 4 35
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now