?
创建方法
?
1.boolean createNewFile() 不存在返回true 存在返回false
2.boolean mkdir() 创建目录
3.boolean mkdirs() 创建多级目录
删除方法
1.boolean delete()
2.boolean deleteOnExit() 文件使用完成后删除
?
public class FileDemo2 {
??? public static void main(String[] args){
??????? File f =new File("d:\\1.txt");
??????? try {
??????????? System.out.println(f.createNewFile());//当文件存在时返回false
??????????? System.out.println(f.delete());//当文件不存在时返回false
??????? } catch (IOException e) {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? }
??? }
}
?
判断方法
1.boolean canExecute()判断文件是否可执行
2.boolean canRead()判断文件是否可读
3.boolean canWrite() 判断文件是否可写
4.boolean exists() 判断文件是否存在
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断
获取方法
1.String getName()
2.String getPath()
3.String getAbsolutePath()
4.String getParent()//如果没有父目录返回null
5.long lastModified()//获取最后一次修改的时间
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//获取机器盘符
9.String[] list()
10.String[] list(FilenameFilter filter)
--------------------------------例如:----?-----------------------------------
private static final String PROJECT_DIR = "E:\\eclipsework\\dep";??//路径
??? private static int fileNum = 0;?
??? private static int lineNum = 0;?
?
??? private static void listNext(File dir) {?
??????? File[] files = dir.listFiles();?
?
??????? for (int i = 0; i < files.length; i++) {?
??????????? if (files[i].isDirectory()) {?
??????????????? listNext(files[i]);?
??????????? } else {?
??????????????? // System.out.println(fs[i].getAbsolutePath());?
??????????????? try {?
??????????????????? if (files[i].getName().endsWith(".xml")) {??//文件类型
??????????????????????? fileNum++;?
??????????????????????? BufferedReader br = new BufferedReader(new FileReader(?
??????????????????????????????? files[i]));?
??????????????????????? while (br.readLine() != null) {?
??????????????????????????? lineNum++;?
??????????????????????? }?
??????????????????? }?
??????????????? } catch (FileNotFoundException e) {?
??????????????????? e.printStackTrace();?
??????????????? } catch (IOException e) {?
??????????????????? e.printStackTrace();?
??????????????? }?
??????????? }?
??????? }?
??? }?
?
??? public static void main(String[] args) throws Exception {?
??????? File root = new File(PROJECT_DIR);?
??????? listNext(root);?
??????? System.out.println("Java files number: " + fileNum);?
??????? System.out.println("Java code lines: " + lineNum);?
??? }?