public class File extends Object implements Serializable, Comparable<File>
构造方法:
public File(String pathname) |
按照完整路径实例化 |
public File(File parent, String child) |
按照父路径和文件名实例化 |
成员:
public static final String separator |
目录分隔符 (‘\‘、‘/‘等) |
public static final String pathSeparator |
多路径切换符(‘;‘、‘:‘等) |
实例:
package wiki.jjcc.test.file; import java.io.File; public ????public ????????System.out.println("windows系统中"); ????????System.out.println("File.pathSeparator="+"["+File.pathSeparator+"]"); ????????System.out.println("File.separator="+"["+File.separator+"]"); ????} } |
windows系统中
File.pathSeparator=[;]
File.separator=[\]
常用方法:
方法 |
功能 |
public boolean createNewFile()throws IOException |
创建新文件 |
public boolean delete() |
删除文件 |
public boolean exists() |
判断文件是否存在 |
public String getName() |
取得文件名 |
public String getParent() |
取得父路径 |
public File getParentFile() |
取得父路径 |
public String getPath()? |
取得完整路径 |
public boolean isDirectory()? |
判断是否为目录 |
public boolean isFile()? |
判断是否为文件 |
public long lastModified()? |
最后修改日期 |
public long length()? |
取得文件大小 |
public File[] listFiles()? |
取得所有 |
public String[] list()? |
? |
public boolean mkdir()? |
创建单个目录 |
public boolean mkdirs()? |
创建多级目录 |
public boolean renameTo(File dest)? |
文件重命名 |
public String toString()? |
将File对象转换为String对象 |
?
实例1:
package wiki.jjcc.test.file; import java.io.File; import java.io.IOException; public ????public ????????String sep = File.separator; ????????File file1 = new File("d:"+sep+"temp"+sep+"test.txt"); ????????if(file1.exists()){ ????????????file1.delete(); ????????}else{ ????????????try { ????????????????file1.createNewFile(); ????????????} catch (IOException e) { ????????????????e.printStackTrace(); ????????????} ????????} ????} } |
以上例子判断相应文件是否存在,若存在则删除文件,若不存在则创建文件。
?
?
实例2:
package wiki.jjcc.test.file; import java.io.File; import java.io.IOException; public ????public ????????String sep = File.separator; ????????File file1 = new File("d:"+sep+"temp"+sep+"adir"); ????????if(file1.exists()){ ????????????file1.delete(); ????????}else{ ????????????try { ????????????????file1.createNewFile(); ????????????} catch (IOException e) { ????????????????e.printStackTrace(); ????????????} ????????} ????} } |
当"adir"文件夹存在时会删除,当文件夹不存在时会新建一个普通文件。
?
实例3:列出D盘下所有文件
package wiki.jjcc.test.file; ? import java.io.File; ? public class FileTest4 { ????public static void main(String[] args) { ????????String sep = File.separator; ????????File file1 = new File("d:"+sep+"temp"+sep+"test.txt"); ????????File file2 = new File("D:\\"); ????????for(File x :File.listRoots()){ ????????????System.out.println(x); ????????} ????????list(file2); ????} ????public static void list(File f) { ????????if (f.isDirectory()) { ????????????File[] temp = f.listFiles(); ????????????//因为磁盘下有些系统目录无法访问,故需要加上以下判断 ????????????if(temp!=null){ ????????????????for (File x : temp) { ????????????????????list(x); ????????????????} ????????????} ????????} else { ????????????System.out.println(f); ????????} ????} } |
实例4:列出D盘下文件名包含java的所有的文件
package wiki.jjcc.test.file; ? import java.io.File; ? public class FileTest4 { ????public static void main(String[] args) { ????????File file2 = new File("D:\\"); ????????list(file2,"java"); ????} ????public static void list(File f,String keyWord) { ????????if (f.isDirectory()) { ????????????File[] temp = f.listFiles(); ????????????//因为磁盘下有些系统目录无法访问,故需要加上以下判断 ????????????if(temp!=null){ ????????????????for (File x : temp) { ????????????????????list(x,keyWord); ????????????????} ????????????} ????????} else { ????????????if(f.getName().contains(keyWord)){ ????????????????System.out.println(f); ????????????} ????????} ????} } |