Results 1 to 5 of 5
- 06-29-2011, 09:18 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
Wait for a file to finish writing before reading.
I feel like this should be a simple solution, but the code I've created so far to deal with it is ugly. My problem is that I'm running the following command to give me a listing of files on a server using Winscp:
Once this file is created, I want to parse it to determine which files from the directory I want to download. The parsing works when the file already exists (but I want the program to delete it every time) or if I give the application a Thread.wait(); call that is long enough for the file to conclude writing. However, if I do not wait, the program executes and fails to download the files because the file is not completely written when it begins to parse.Java Code:Process p = rt.exec("cmd /c winscp.com <connection> /command ls > ./bin/temp.txt close exit");
A third option I'm working with is:
But again, I don't like this method even if it works because it's ugly and uses arbitrary Thread.wait() calls. How can I cleanly determine when the file is done writing and that it is safe to parse? Ideas?Java Code:long prevLength = 0; Thread.sleep(300); long newlength = file.length(); System.out.println("Previous size: " + prevLength); // print out the size for testing System.out.println("New size: " + newlength);// print out the size for testing while(prevLength < newlength){ prevLength = newlength; Thread.sleep(250); newlength = file.length(); System.out.println("Previous size: " + prevLength);// print out the size for testing System.out.println("New size: " + newlength);// print out the size for testing }Last edited by unaligned; 06-29-2011 at 09:39 PM.
- 06-29-2011, 09:35 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Rather than pipe it to a file, you could try to get the InputStream of the process and read this stream...if you wrap the stream in a Reader, the read method will return the appropriate cue to exit a reading loop when the process is complete (for example, using a BufferedReader the readLine should return null). You then will have the output in memory already and a cue when the process is complete.
- 06-29-2011, 10:07 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
- 06-29-2011, 11:47 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
I wrapped the input (aka: output? why so confusing Java?) from the process in a reader as you suggested and it works just as it did before with very few changes... and much more efficiently I might add. Thanks for the help.
Last edited by unaligned; 06-29-2011 at 11:50 PM.
- 06-30-2011, 09:46 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Reading from .xls file and writing to .csv
By Yatta in forum New To JavaReplies: 1Last Post: 04-09-2011, 04:44 PM -
File reading / writing
By MattBSibley in forum New To JavaReplies: 5Last Post: 04-19-2010, 05:20 AM -
Reading and writing to a file
By jigglywiggly in forum New To JavaReplies: 13Last Post: 03-09-2009, 10:44 AM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks