java的文件删除和重命名,java中进行文件的删除和重命名

删除文件:

import java.io.File;

public class Delete {

public static void main(String[] args) {

String fileName = "file.txt";

// A File object to represent the filename

File f = new File(fileName);

// Make sure the file or directory exists and isn't write protected

if (!f.exists())

throw new IllegalArgumentException(

"Delete: no such file or directory: " + fileName);

if (!f.canWrite())

throw new IllegalArgumentException("Delete: write protected: "

+ fileName);

// If it is a directory, make sure it is empty

if (f.isDirectory()) {

String[] files = f.list();

if (files.length > 0)

throw new IllegalArgumentException(

"Delete: directory not empty: " + fileName);

}

// Attempt to delete it

boolean success = f.delete();

System.out.println("delete file finish!!!!");

if (!success)

throw new IllegalArgumentException("Delete: deletion failed");

}

}

-------------------------------------------------------------------------------------------------------------------------------------

文件重命名 [root@dbtest2 ~]# more Rename.java import java.io.File; import java.io.IOException; public class Rename {   public static void main(String[] argv) throws IOException {     // Construct the file object. Does NOT create a file on disk!     File f = new File("wade123.txt"); // backup of this source file.     // Rename the backup file to "junk.dat"     // Renaming requires a File object for the target.     f.renameTo(new File("junk.dat"));   } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值