File类是文件和目录路径名的抽象表示形式,使用这个类可以创建文件或目录,以及计算这个文件的大小,修改时间等等。
File类常用的三种构造方法:
/*
* 第一种File类的构造方法
*/
File fileone = new File("D:/testone.txt");
//创建新文件 将会抛出强制捕获的异常,使用try...catch块来进行捕获
try {
fileone.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 第二种File类的构造方法
*/
File dir = new File("D:/");
String name = "testtwo.txt";
File filetwo = new File(dir, name);
try {
filetwo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 第三种构造方法
*/
File filethree = new File("D:/", "testthree.txt");
try {
filethree.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
以上只是构造方法,有了构造方法就可以长生对象,从而可以执行其中具有的方法,可以判断文件是否是文件还是目录,是否是隐藏文件,是否可读,可写,可执行等等,具体的方法可以看帮助文档,在这就不啰嗦了。