Home >Java >javaTutorial >Java Example - Delete File

Java Example - Delete File

黄舟
黄舟Original
2017-02-15 10:07:351517browse

以下实例演示了使用 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();
      }
    }}

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

test.txt 文件已被删除!

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

Statement:
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