文件创建删除类.txt

来自「一些自己平时积累的小知识点, java 的 javaee 的」· 文本 代码 · 共 68 行

TXT
68
字号

 
import  java.io.BufferedWriter;  
import  java.io.File;  
import  java.io.FileWriter;  
import  java.io.IOException;  
public  class    CreateNewFile  {  
           /**  
           *  删除文件  
           */  
           public  void  deleteFile(String  path){  
                       File  f=new  File(path);    //path:E:/tomcat/...  
                       try{  
                                     f.delete();  
                       }catch(Exception  e){}  
           }  
 
           /**  
           *创建文件  
           */  
           public  boolean  createFile(String  _path,  String  _fileName)    
                                         throws  IOException  {  
                       boolean  success  =  false;  
                       String  fileName  =  _fileName;  
                       String  filePath  =  _path;  
                       try  {  
                                   File  file  =  new  File(filePath);  
                                   if  (!file.isDirectory())  {  
                                               file.mkdirs();  
                                   }  
                                   File  f  =  new  File(filePath  +  File.separator    
                                                                       +  fileName  +  ".txt");  
                                   success  =  f.createNewFile();  
 
                       }  catch  (IOException  e)  {  
                                   e.printStackTrace();  
                       }  
                       return  success;  
 
           }  
 
           /**  
           *  向文件中写入数据  
           */  
           public  void  writeFile(String  _path,  String  _fileName,  
                                                     String  insertString,boolean  isLast)    
                                                     throws  IOException  {  
 
                       try  {  
                                   BufferedWriter  out  =  new  BufferedWriter(new  FileWriter(_path  +  File.separator  +  _fileName  +  ".txt"));  
                                   out.write(insertString);  
                                   if(!isLast)  out.newLine();  
                                   out.flush();  
                                   out.close();  
                       }  catch  (IOException  e)  {  
                                   e.printStackTrace();  
                       }  
 
           }  
 
}  
 
 
 
---------------------------------------------------------------  
 
File  file  =  new  File("d:\asd");  
file.mkdir();或者file.mkdirs()  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?