1 package com.mon11.day16.file;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 /**
7 * 类说明 :
8 * @author 作者 :chenyanlong
9 * @version 创建时间:2017年11月16日
10 */
11 public class Fileoper1 {
12
13 public static void main(String[] args) {
14
15 Fileoper1 fm=new Fileoper1();
16 File file=null;
17 file =new File("C:\\Users\\Administrator\\Desktop\\3.txt");
18 try {
19 file.createNewFile();
20 } catch (IOException e) {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 }
24
25 }
26
27 /**
28 * 创建类的方法
29 * @param file文件对象
30 */
31 public void create(File file){
32 if(!file.exists()){
33 try {
34 file.createNewFile();
35 System.out.println("文件已经创建");
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 }
41
42 /**
43 * 显示文件信息
44 * @param file文件对象
45 */
46 public void showFileInfo(File file){
47 if(file.exists()){//判断文件是否存在
48 if(file.isFile()){//如果是文件
49 System.out.println("名称:"+file.getName());
50 System.out.println("相对路径:"+file.getPath());
51 System.out.println("绝对路径:"+file.getAbsolutePath());
52 System.out.println("文件大小:"+file.length()+"字节");
53 }
54
55 if(file.isDirectory()){
56 System.out.println("此文件是目录");
57 }
58
59 }else{
60 System.out.println("文件不存在");
61 }
62 }
63
64 /**
65 * 删除文件
66 * @param file文件对象
67 */
68 public void delete(File file){
69 if(file.exists()){
70 file.delete();
71 System.out.println("文件已删除");
72 }
73 }
74
75 }