Java中File的相关操作


Creation of a file if it does not exist; Deletion of a file if it already exists.

  1. package com.han;  
  2.   
  3. import java.io.File;  
  4.   
  5. /** 
  6.  * Creation of a file if it does not exist; 
  7.  * Deletion of a file if it already exists. 
  8.  * @author han 
  9.  * 
  10.  */  
  11. public class FileTest {  
  12.   
  13.     public static void main(String[] args) {  
  14.         // TODO Auto-generated method stub   
  15.         File file=new File("/home/han/Documents","word.txt");  
  16.         if(file.exists()){  
  17.             System.out.println(file.getPath());  
  18.             file.delete();  
  19.             System.out.println("文件已删除");  
  20.         }else{  
  21.             try{  
  22.                 file.createNewFile();  
  23.                 System.out.println("文件已创建");  
  24.             }catch(Exception e){  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.   
  29.     }  
  30.   
  31. }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页

相关内容