java文件操作

来源:互联网 发布:mac 主题更换 编辑:程序博客网 时间:2024/05/17 22:53
import java.io.File;import java.io.IOException;/* * 文件操作:创建、查看文件相关信息、删除  */public class FileMethods {public void createFile(File file) {if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("文件创建成功");}}// 查看文件相关信息public void showFileInfo(File file) {if (file.exists()) {if (file.isFile()) {// 是文件System.out.println("文件名" + file.getName());System.out.println("文件绝对路径" + file.getAbsolutePath());System.out.println("文件相对路径" + file.getPath());System.out.println("文件大小" + file.length());}if (file.isDirectory()) {// 是目录System.out.println("这是一个目录");}}}//删除文件public void deleteFile(File file ){if (file.exists()) {System.out.println("文件删除成功");}}public static void main(String[] args) {FileMethods fm=new FileMethods();File file=new File("D:\\701\\hello.txt");fm.createFile(file);//查看文件信息fm.showFileInfo(file);}}

0 0
原创粉丝点击