Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Deleting a File

Java Example - Deleting a File

Java Examples Java Examples

The following example demonstrates how to delete a file using the delete() method:

Main.java File

import

java

.

io

.*;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

try

{

File

file

=

new

File

(

"

c:

\

test.txt

"

)

;

if

(

file

.

delete

(

)

)

{

System

.

out

.

println

(

file

.

getName

(

)

"

The file has been deleted!

"

)

;

}

else

{

System

.

out

.

println

(

"

File deletion failed!

"

)

;

}

}

catch

(

Exception

e

)

{

e

.

printStackTrace

(

)

;

}

}

}

The output of the above code is (you need to create a test.txt file on the C: drive first):



test.txt 文件已被删除!

Java Examples Java Examples