File类是java中表示文件和目录名的抽象表示形式。File类可以实现文件的创建,删除,重命名,获取路径,创建时间等等,是唯一一个与文件本身有关的操作。所以有必要好好学习File类。
主要使用方法:
1.public File(String pathname)----根据文件路径构造File实例
2.public boolean createNewFile()throws IOException-----创建文件
3.public boolean delete()----------删除文件
4.public String getParent()----------得到文件的上一级路径
5.public boolean isDirectory()------判断是不是文件夹
6.public boolean isFile()------判断是不是文件
7.public String[]list()------------列出文件夹中的文件名
8.public File[] listFile()------------列出文件夹的所有文件实例
9.public boolean mkdir()----------创建文件夹
10.public boolean renameTo(File dest)-----文件重命名(可以理解移动文件)
11.public long length()-------返回文件大小
12.String getpath()-----------路径名字符串
示例代码:
package com;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//File.separator是跨平台的,在windows下为D:\1.txt,linux下为D:/1.txt <br style="word-wrap:break-word">
File file=new File("D:"+File.separator+"1.txt"); <br style="word-wrap:break-word">
//文件不存在 <br style="word-wrap:break-word">
if(!file.exists()) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
try { <br style="word-wrap:break-word">
if(file.createNewFile())//创建文件 <br style="word-wrap:break-word">
System.out.println("文件创建成功"); <br style="word-wrap:break-word">
} catch (IOException e) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
e.printStackTrace(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//得到文件的上一级路径 <br style="word-wrap:break-word">
System.out.println("上级目录:"+file.getParent()); <br style="word-wrap:break-word">
//判断是否是目录 <br style="word-wrap:break-word">
System.out.println("是否是目录:"+file.isDirectory()); <br style="word-wrap:break-word">
//判断是否是文件 <br style="word-wrap:break-word">
System.out.println("是否是文件:"+file.isFile()); <br style="word-wrap:break-word">
//获取文件大小 <br style="word-wrap:break-word">
System.out.println("文件大小为:"+file.length()); <br style="word-wrap:break-word">
//文件路径 <br style="word-wrap:break-word">
System.out.println(file.getPath()); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//重命名,返回值为true时。相当于把D:\1.txt移动到L:\kk22.php <br style="word-wrap:break-word">
if(file.renameTo(new File("L:\kk22.php"))) <br style="word-wrap:break-word">
System.out.println("重命名成功"); <br style="word-wrap:break-word">
else { <br style="word-wrap:break-word">
System.out.println("重命名失败"); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//指向重命名的文件 <br style="word-wrap:break-word">
file=new File("L:\kk22.php"); <br style="word-wrap:break-word">
if(file.delete()) <br style="word-wrap:break-word">
System.out.println("文件删除成功"); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//--------------------------------------目录演示 <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
file=new File("L:"+File.separator+"javaTest"); <br style="word-wrap:break-word">
//不存在 <br style="word-wrap:break-word">
if(!file.exists()) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
file.mkdir();//创建该目录 <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//为该目录添加文件 <br style="word-wrap:break-word">
String temp; <br style="word-wrap:break-word">
for(int i=0;i<4;i++) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
temp=""; <br style="word-wrap:break-word">
temp="L:"+File.separator+"javaTest"+File.separator+i+"txt"; <br style="word-wrap:break-word">
File ok=new File(temp); <br style="word-wrap:break-word">
try { <br style="word-wrap:break-word">
ok.createNewFile();//创建该文件 <br style="word-wrap:break-word">
} catch (IOException e) { <br style="word-wrap:break-word">
// TODO Auto-generated catch block <br style="word-wrap:break-word">
e.printStackTrace(); <br style="word-wrap:break-word">
}// <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//获取该目录下的文件名 <br style="word-wrap:break-word">
String[] name=file.list(); <br style="word-wrap:break-word">
for(int i=0;i<4;i++) <br style="word-wrap:break-word">
System.out.println(name[i]); <br style="word-wrap:break-word">
//获取该目录下的文件实例 <br style="word-wrap:break-word">
File[] files=file.listFiles(); <br style="word-wrap:break-word">
for(int i=0;i<4;i++) <br style="word-wrap:break-word">
System.out.println(files[i].getPath()); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
}
复制代码
File只能获取文件路径创建时间等信息以及文件的创建和删除,不能对文件里面的具体内容实现获取和修改。
获取和修改文件的具体内容需要用到java的IO流。之后会讲解IO相关的操作。