您的位置:首页 > 编程语言 > Java开发

java File类的基本操作

2014-05-01 09:45 323 查看
上代码
import java.io.*;

public class FileDemo
{
	public static void main(String[] args)
	{
		File f=new File("F:\\workspace\\JavaPrj\\test.txt");
		if(f.exists())
		{
			f.delete();
		}
		else
		{
			try
			{
				f.createNewFile();	
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
		//文件名
		System.out.println("The file name is "+f.getName()+".");
		//路径名
		System.out.println("The path is "+f.getPath()+".");
		//绝对路径名
		System.out.println("The absolute path is "+f.getAbsolutePath()+".");
		//父目录
		System.out.println("The father directory is "+f.getParent()+".");
		//文件是否存在
		System.out.println(f.exists()?"The file exists.":"The file does not exist.");
		//文件是否可写
		System.out.println(f.canWrite()?"The file can been writen.":"The file can not been writen.");
		//文件是否可读
		System.out.println(f.canRead()?"the file can been read":"The file can not been read.");
		//文件句柄是否是一个目录
		System.out.println("The file "+(f.isDirectory()?"is":"is not")+" a directory.");
		//文件句柄是否是一个绝对路径
		System.out.println("The object "+(f.isAbsolute()?"is an absolute path":"is not an absolute path"));
		//文件最后被修改的时间
		System.out.println("The file has been modified at "+f.lastModified()+".");
		//文件长度
		System.out.println("The length of the file is "+f.length()+" Bytes.");
	}
}

一次执行



二次执行

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: