现在的位置: 首页 > 综合 > 正文

java 创建文件和目录

2013年10月11日 ⁄ 综合 ⁄ 共 619字 ⁄ 字号 评论关闭
 

public static void main(String[] args){
     

        
try{
            
/*查找目录,如果不存在,就创建*/
            File dirFile 
= new File("e:/test");
            
if(!dirFile.exists()){
                
if(!dirFile.mkdir())
                    
throw new Exception("目录不存在,创建失败!");
            }

            
/*查找文件,如果不存在,就创建*/
            File file 
= new File("e:/test/text1.txt");
            
if(!file.exists())
                
if(!file.createNewFile())
                    
throw new Exception("文件不存在,创建失败!");
                
            
        }
catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

抱歉!评论已关闭.