Results 1 to 5 of 5
  1. #1
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default 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:

    Java Code:
    Process p = rt.exec("cmd /c winscp.com <connection> /command ls > ./bin/temp.txt close exit");
    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.

    A third option I'm working with is:
    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
    }
    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?
    Last edited by unaligned; 06-29-2011 at 09:39 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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.

  3. #3
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    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.
    Thank you for this suggestion... I hadn't considered it. In 3 lines of set-up code I have replaced all the ugliness that my previous code had.

  4. #4
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by unaligned View Post
    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.
    Your Java code is reading it, hence it is input.
    It's output from the Process yes, but for your program it is input.

Similar Threads

  1. Reading from .xls file and writing to .csv
    By Yatta in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 04:44 PM
  2. File reading / writing
    By MattBSibley in forum New To Java
    Replies: 5
    Last Post: 04-19-2010, 05:20 AM
  3. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  4. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •