Java中File的相关操作
Java中File的相关操作
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();
- }
- }
- }
- }
|
评论暂时关闭