Results 1 to 5 of 5
  1. #1
    tmotse is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default Threads and File I/O

    Hi Guys please help me here: i need this to delete a file after reading it:

    public class Reporter extends Thread {

    /**
    * Start a thread that waits 10sec then read file and delete after that.
    */
    public void run() {
    String noCar = "Nothing on the file at the moment, then thing to print on console ";
    String carExists = "Printing Car on Console";

    while (true) {

    try {
    FileInputStream fis = new FileInputStream("C:/Car/car.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Car carFromFile = (Car) ois.readObject();//de-serialize Car object
    ois.close();




    if (carFromFile != null) {
    System.out.println(carExists);
    System.out.print(carFromFile);


    } else {
    try {
    System.out.println(noCar);
    // sleep for 10sec

    sleep(10);
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    To delete a file you need a handle to the File object and call the File.delete method.
    Why are you making this class extend Thread?

  3. #3
    tmotse is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    It is extending a thread because i have a folder watcher which run concurrently within the swing app, and the thread has to poll the folder every 10 secs
    If a new file is detected, it is deserialized and the contents are output to the Console
    Once the file is output to the Console it must be deleted

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The answer still applies.
    It's more appropriate to implement runnable than to extend Thread though.
    You may also find a search for open source java file system monitoring projects to be useful.

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    sleep(10) sleeps for 10 milliseconds, not 10 seconds.

    to delete a file: new File(fileName).delete ();

Similar Threads

  1. Threads
    By GoLuM83 in forum Advanced Java
    Replies: 7
    Last Post: 03-31-2010, 09:52 PM
  2. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  3. Threads reading a file and writing
    By akronjava in forum Threads and Synchronization
    Replies: 6
    Last Post: 09-07-2009, 03:52 PM
  4. FileLock and file access from concurrent threads..
    By fxRichard in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2009, 09:08 PM
  5. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 05:11 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
  •