<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">package com.hfxt;
import java.io.File;
import java.io.IOException;
//执行 创建文件 查询文件相关信息 删除文件
public class FileDemo {
//创建文件
public void create(File file) {
//当文件不存在时,创建文件
if (!file.exists()) {
try {
file.createNewFile();
System.out.println("文件已创建!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查看文件
public void showInfo(File file){
if(file.exists()){
//文件存在,则查看其信息
if(file.isFile()){
System.out.println("该文件名:"+file.getName());
System.out.println("相对路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("文件大小:"+file.length()+"字节");
}
if(file.isDirectory()){
System.out.println("此文件是目录");
}
}else{
System.out.println("文件不存在!");
}
}
//删除文件
public void dele(File file){
if(file.exists()){
//如果文件存在
file.delete();
System.out.println("文件删除成功!");
}else{
System.out.println("文件删除失败!");
}
}
public static void main(String[] args) {
FileDemo fd = new FileDemo();
File file = new File("F:/fangchen/test.txt"); //新建文件储存路劲
//创建文件
fd.create(file);
//查看文件
//fd.showInfo(file);
//删除文件
//fd.dele(file);
}
}
</span></strong>