Home > Java > javaTutorial > Java Example - Delete File

Java Example - Delete File

黄舟
Release: 2017-02-15 10:07:35
Original
1530 people have browsed it

以下实例演示了使用 delete() 方法将文件删除:

/*
 author by w3cschool.cc
 Main.java
 */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() + " 文件已被删除!");
            }else{
               System.out.println("文件删除失败!");
         }
      }catch(Exception e){
         e.printStackTrace();
      }
    }}
Copy after login

以上代码运行输出结果为(需要在 C 盘上先创建 test.txt 文件):

test.txt 文件已被删除!
Copy after login

以上就是Java 实例 - 删除文件的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template