歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中File的相關操作

Java中File的相關操作

日期:2017/3/1 10:35:53   编辑:Linux編程

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

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