Creation of a file if it does not exist; Deletion of a file if it already exists.
- package com.han;
-
- import java.io.File;
-
- /**
- * Creation of a file if it does not exist;
- * Deletion of a file if it already exists.
- * @author han
- *
- */
- public class FileTest {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File file=new File("/home/han/Documents","word.txt");
- if(file.exists()){
- System.out.println(file.getPath());
- file.delete();
- System.out.println("文件已刪除");
- }else{
- try{
- file.createNewFile();
- System.out.println("文件已創建");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- }
-
- }