课堂所讲整理:输入输出流(I/O)

 1 package org.hanqi.ex;
 2 
 3 import java.io.*;
 4 
 5 public class TestFile {
 6 
 7     public static void main(String[] args) {
 8         
 9         //构建File实例,指向了硬盘上的一个文件
10         File file = new File("d:\test.txt");
11         file = new File("d:\","test1.txt");
12         //判断一下文件是否存在
13         if(file.exists())
14         {
15             System.out.println(file.getName()+"文件已存在");
16             System.out.println(file.getAbsolutePath());
17             //改名
18             file.renameTo(new File("e:\test3.txt"));
19 //            try {
20 //                if(file.createNewFile())
21 //                {
22 //                    System.out.println("创建文件成功");
23 //                }
24 //                else
25 //                {
26 //                    System.out.println("创建失败");
27 //                }
28 //            } catch (IOException e) {
29 //                // TODO 自动生成的 catch 块
30 //                e.printStackTrace();
31 //                System.out.println(e.getMessage());
32 //            }
33 //            if(file.delete())
34 //            {
35 //                System.out.println("删除成功");
36 //            }
37 //            else
38 //            {
39 //                System.out.println("删除失败");
40 //            }
41         }
42         else
43         {
44             System.out.println("文件不存在");
45             //创建
46             try {
47                 if(file.createNewFile())
48                 {
49                     System.out.println("创建文件成功");
50                 }
51                 else
52                 {
53                     System.out.println("创建失败");
54                 }
55             } catch (IOException e) {
56                 
57                 System.out.println(e.getMessage());
58                 e.printStackTrace();
59             }
60         }
61     }
62 }

附相关思维导图:

原文地址:https://www.cnblogs.com/hanazawalove/p/5272187.html