Welcom to RO_wsy's blog

java中File类用法

import java.io.*;
import java.util.*;

public class FileTest {
	public static void main(String [] args) {
		File f = new File("1.txt");
		
		if (f.exists()) {
			f.delete();
		} else {
			try {
				f.createNewFile();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("File name " + f.getName());
		System.out.println("File path " + f.getPath());
		System.out.println("File absolute path " + f.getAbsolutePath());
		System.out.println("File parent " + f.getParent());
		System.out.println("File last modified " + f.lastModified());
		System.out.println("is exist " + (f.exists() ? "exist" : "not exist"));
		System.out.println("can read " + (f.canRead() ? "yes" : "no"));
		System.out.println("is directory " + (f.isDirectory() ? "yes" : "no"));
		
		Date date = new Date(f.lastModified());
		
		System.out.println(date); // print modified date
	}
}

其他函数请自行参照jdk文档查询
posted @ 2012-11-20 20:02  RO_wsy  阅读(220)  评论(0编辑  收藏  举报