输入输出I/O

 1 package IO;
 2 import java.io.*;
 3 public class TestFile {
 4 
 5     public static void main(String[] args) {
 6         //构建File实例,指向了硬盘上的一个文件
 7         // TODO Auto-generated method stub
 8         File file=new File("e:\test.txt");
 9         file=new File("d:\","test1.txt");
10         //判断文件是否存在
11         if(file.exists())
12         {
13             System.out.println(file.getName()+"文件已存在");
14             //输出路径
15             System.out.println(file.getAbsolutePath());
16             
17             //改名
18             file.renameTo(new File("f:\test4.txt"));
19             
20             
21 //            try {
22 //                if(file.createNewFile())
23 //                {
24 //                    System.out.println("创建文件成功");
25 //                }
26 //                else
27 //                {
28 //                    System.out.println("创建文件失败");
29 //                }
30 //            } catch (IOException e) {
31 //                // TODO Auto-generated catch block
32 //                e.printStackTrace();
33 //            }
34 //            if(file.delete())
35 //            {
36 //                System.out.println("删除成功");
37 //            }
38 //            else
39 //            {
40 //                System.out.println("删除失败");
41 //            }
42         }
43         else
44         {
45             System.out.println(file.getName()+"文件不存在");
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                 // TODO Auto-generated catch block
57                 System.out.println(e.getMessage());
58                 e.printStackTrace();
59             }
60         }
61         
62     }
63 
64 }
View Code

原文地址:https://www.cnblogs.com/beens/p/5274491.html