要求是每隔一段时间(1分钟)将指定文件移到指定目录下!现已经有程序对该目录下的文件处理。
 该如何实现?

解决方案 »

  1.   

    如果是用ftp类的话可以借助现成的方法实现
    java中文件拷贝用流的方式
    如果是同一机器上的文件拷贝可以用
    File.rename()方法
      

  2.   

    public class MoveFile {
    public static void main(String[] args) throws IOException {
    //set timer
    Timer timer = new Timer();
    long timeIterval = Long.valueOf(args[2]).longValue();

    //System.out.println("test iterval" + timeIterval);
    //timeInterval = args[3];
    if (args[1] != null && args[1].length() != 0) {
    if (args[0] != null && args[0].length() != 0) {
    File inputFile = new File(args[0]);
    if (inputFile.exists()) {
    RunTimer runTimer = new RunTimer(args[0], args[1]);
    timer.scheduleAtFixedRate(runTimer, 500, timeIterval);
    } else {
    System.out.println("file not existed");
    }
    }
    }
    }
    }class RunTimer extends TimerTask {
    int i = 0; String inputFileName = ""; String outputFileName = ""; /**
     * @param string
     */
    public RunTimer(String inputDir, String outPutDir) {
    inputFileName = inputDir;
    outputFileName = outPutDir;
    // TODO Auto-generated constructor stub
    } public void run() {
    FileInputStream fin = null;
    try {
    //fin = new FileInputStream("E:\\test.txt");
    fin = new FileInputStream(inputFileName);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    File fcopy = new File(outputFileName);
    try {
    if (fcopy.exists()) {
    System.out.println("file already existed");
    } else {
    fcopy.createNewFile();
    }
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    FileOutputStream fout = null;
    try {
    fout = new FileOutputStream(fcopy);
    } catch (FileNotFoundException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    int stae;
    try {
    while ((stae = fin.read()) != -1) {
    fout.write(stae);
    }
    fout.close();
    } catch (IOException e3) {
    // TODO Auto-generated catch block
    e3.printStackTrace();
    }
    System.out.println("" + (i++));
    }
    }