import java.io.*;class filetest{public static void main(String[] args){ try { /**//*查找目录,如果不存在,就创建*/ File dirFile = new File("e://test");//或者File dirFile = new File("e:/test"); if(!dirFile.exists()) { if(!dirFile.mkdir()) throw new Exception("目录不存在,创建失败!"); } /**//*查找文件,如果不存在,就创建*/ File file = new File("e://test//text1.txt");//或者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()); } } }