大体思路:
每隔2秒检查d:/temp/aa.txt是否存在 最多检查5次 如果存在就读取并返回 
现在问题是只要执行完timer.schedule(new timerTask(), 0, 2000);
就执行 return content; 根本不等到5次检查完 请问如何解决?谢谢 (不要说用Thread 有原因不能用)
package cn.wangy.io;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class IOTest {
private String path = "d:/temp/aa.txt";
private int times = 5;
Timer timer = null;
private String content = "aa"; public String doTrade() {
timer = new Timer();
timer.schedule(new timerTask(), 0, 2000);   //这里出问题
return content;       } /**
 * 内部类 用来查看rev文件夹下是否有需要的文件 有就读取后返回
 * 
 * @author Owner
 * 
 */
class timerTask extends TimerTask { @Override
public void run() {
if (times > 0) {
if (checkFile()) {
readFileContent();
timer.cancel();
} else {
times--;
}
} else {
timer.cancel();
}
} } /**
 * 检查rev文件夹中 文件是否存在
 * 
 * @return
 */
public boolean checkFile() {
File file = new File(path);
if (file.exists()) {
return true;
}
return false;
} /**
 * 读取文件内容 然后存入content 删除文件
 */
public void readFileContent() {
File file = new File(path);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
content += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
file.deleteOnExit(); // 删除文件
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package cn.wangy.io;public class Main { /**
 * @param args
 */
public static void main(String[] args) {
/**
 * @param args
 */
String content = new IOTest().doTrade();
System.out.println(content);
}}

解决方案 »

  1.   

    你的理解有点问题Timer就是Thread他绝对不会等待timer完成的
    timer.schedule(new timerTask(), 0, 2000);   //这里出问题
    return content;lz的需求如果是等待完成的话  不需要Thread 也不需要Timer
      

  2.   


    public String doTrade() {
    while (times  > 0) {
    Thread.sleep(2000);
    if (checkFile()) {
    readFileContent();
    } else {
    times--;
    }
    }
    return content;
    }
    只需如此
      

  3.   

    作了些小改动,其实重点是你用的方法错了package com.ecc.test1016;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;public class IOTest { private String path = "C:\\j2sdk1.4.2\\README.txt";
    private int times = 5;
    private String content = "aa";
    Timer timer = new Timer();

    public void doTrade() {
    TimerTask timerTask = new timerTask();
    timer.scheduleAtFixedRate(timerTask, 0, 3000); // 正好在3秒以后终止
    // timer.schedule(timerTask, 0, 3000); // 保持间隔时间的稳定,每一秒鐘執行一次,可能超過3秒 } /**
     * 内部类 用来查看rev文件夹下是否有需要的文件 有就读取后返回
     * 
     * @author Owner
     * 
     */
    class timerTask extends TimerTask {
    public void run() {
    if (times > 0) {
    if (checkFile()) {
    readFileContent();
    System.out.println(content);
    timer.cancel();
    } else {
    times--;
    }
    } else {
    timer.cancel();
    }
    } } /**
     * 检查rev文件夹中 文件是否存在
     * 
     * @return
     */
    public boolean checkFile() {
    File file = new File(path);
    if (file.exists()) {
    return true;
    }
    return true;
    } /**
     * 读取文件内容 然后存入content 删除文件
     */
    public void readFileContent() {
    File file = new File(path);
    BufferedReader reader = null;
    try {
    reader = new BufferedReader(new FileReader(file));
    String line = "";
    content = "";
    while ((line = reader.readLine()) != null) {
    content += line + "\n";
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    file.deleteOnExit(); // 删除文件
    try {
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } /**
     * @param args
     */
    public static void main(String[] args) {
    /**
     * @param args
     */
    IOTest ioTest = new IOTest();
    ioTest.doTrade();
    }}
      

  4.   

    (1)2个参数的schedule在制定任务计划时, 如果指定的计划执行时间scheduledExecutionTime<=systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。
    (2)3个参数的schedule在制定反复执行一个task的计划时,每一次执行这个task的计划执行时间随着前一次的实际执行时间而变,也就是scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是说如果第n次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>=scheduledExecutionTime(第n+1次),则此时不做时隔等待,立即执行第n+1次task,而接下来的第n+2次task的scheduledExecutionTime(第n+2次)就随着变成了realExecutionTime(第n+1次)+periodTime。说白了,这个方法更注重保持间隔时间的稳定。
    (3)3个参数的scheduleAtFixedRate在制定反复执行一个task的计划时,每一次执行这个task的计划执行时间在最初就被定下来了,也就是scheduledExecutionTime(第n次)=firstExecuteTime+n*periodTime;如果第n次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>=scheduledExecutionTime(第n+1次),则此时不做period间隔等待,立即执行第n+1次task,而接下来的第n+2次的task的scheduledExecutionTime(第n+2次)依然还是firstExecuteTime+(n+2)*periodTime这在第一次执行task就定下来了。说白了,这个方法更注重保持执行频率的稳定。
    引用
    http://blog.csdn.net/ahxu/archive/2005/01/12/249610.aspx