Java本地文件操作-新建、删除、重命名、属性

13 篇文章 0 订阅
11 篇文章 0 订阅
FileDemo.java


package 极客学院练习;


import java.io.File;
import java.io.IOException;


public class FileDemo {


    public static void main(String[] args) {


        File file = new File("hello.txt");


        File newname = new File("abc.txt");
        file.renameTo(newname); // 文件重命名


        System.out.println(file.exists()); // 测试文件是否存在
        System.out.println(file.isFile()); // 判断是是否文件
        System.out.println(newname.exists());
        System.out.println(newname.isFile());
        try {
            file.createNewFile(); // 创建文件,需要异常处理
            if (file.exists())
                System.out.println("成功创建" + file.getName());
        } catch (IOException e) {


            e.printStackTrace();
        }


    }


}
FolderDemo .java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package 极客学院练习;


import java.io.File;
import java.io.IOException;


public class FolderDemo {


    public static void main(String[] args) {
//      File folder = new File("Demo/ad/ada/ad");
//      if(folder.mkdirs()) // 创建文件夹,return boolean
//          System.out.println(folder + "文件夹创建完成.");
//      else
//      {
//          if(folder.exists())
//              System.out.println("文件夹创建成功.");
//              else
//                  System.out.println("文件夹创建失败.");
//          }
        
        
    
//      File folder1 = new File("Demo/ad/ada");  //文件夹的删除.
//          if(folder1.delete()){
//              System.out.println("done");
//          }
//          else
//          System.out.println("faild");
//
//      }
//          


        File file=new File("1/2/3/4/5");
        File newfile=new File("1/2/3/4/5/6");
        file.mkdirs();   newfile.mkdirs();
        System.out.println(file.renameTo(newfile));  //文件夹的重命名.
        if(file.renameTo(newfile))
            System.out.println("done");
        else
            System.out.println("faild");
        
        
    }
}
ReadFileDemo.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package 极客学院练习;


import java.io.File;


public class ReadFileDemo {


    public static void main(String[] args) {
        File file=new File("wang.txt");
        System.out.println(file.exists());
        System.out.println(file.getName()); 
        System.out.println(file.getAbsolutePath());


        System.out.println(file.getParent());
        System.out.println(file.getPath());
        System.out.println(file.length());
        System.out.println(file.isHidden());
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
    
    }


}
转载请注明来自Bcoder网站所有,本文地址:http://www.bcoder.cn/?p=700 除非注明,Bcoder文章均为原创,转载请注明出处和链接! 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值