result of File exists() method will change even the file object is the same

Here is code snipet that demos the virability of exists method of File.

 

when the program is begin, there is no such file, but when you moved the file in this directory, the File object f can detect the file existance.

/*
 * @(#)FileExist.java	2014-4-17
 *
 * Copyright (c) 2013 LabJ. All rights reserved.
 */

package com.labj.io;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @description
 * 
 * @author jwu
 */
public class FileExist {
	public static void main(String[] args) throws Exception {
		File f = new File("D:\\wutalk\\tmp\\moved-in.txt");
		while (!f.exists()) {
			System.out.println("waiting file move in...");
			TimeUnit.MILLISECONDS.sleep(1000);
		}
		System.out.println(f.getPath() + " is found");
	}

}

 

 So you don't need to create a new File object to test the existance of a file.

你可能感兴趣的:(object)