用java.io.File的方法编写简单的小程序1

5 篇文章 0 订阅
4 篇文章 0 订阅

程序中用到的方法描述

CreateNewFile() 函数用于新建文件

注意事项:
1.文件若已经存在,则不再创建,返回false;
2.文件路径不存在,则会报错:IOException:系统找不到指定的路径

Mkdirs() 函数用于新建多层文件夹 delete() 函数用于删除文件,删除空文件

public class Test {
	
	public static void main(String[] args) throws IOException {
		
		System.out.println("请输入文件路径(格式:d://abc//a//b//c):");
		String a = new Scanner(System.in).nextLine();
		File f1 = new File(a);
		f1.mkdirs();
		
		System.out.println("请输入创建的文件名称:");
		String b = new Scanner(System.in).nextLine();
		File f2 = new File(f1, b);
		f2.createNewFile();
		
		System.out.println("请按回车删除文件"+ b);
		new Scanner(System.in).nextLine();
		boolean q = f2.delete();
		if(true == q) {
			System.out.println("删除成功");
		}else {
			throw new RuntimeException("出错了");
			// 可以将文件名修改,就可以测试报错
		}
		
		String re = a.substring(a.lastIndexOf("/")+1);
		System.out.println("请回车删除"+re+"文件夹");
		new Scanner(System.in).nextLine();
		f1.delete();
		System.out.println("删除成功");
	}

Console:
在这里插入图片描述
文件夹这里就不截图了,自行测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值