`
reginaminlee
  • 浏览: 32575 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

File 创建文件

    博客分类:
  • java
阅读更多

从来没有研究过File类,需要用时,都是网上copy下

今天写段初级代码,放这给自己看看

Java代码 复制代码 收藏代码
  1. import java.io.*;    
  2.   
  3. public class FileTest{   
  4.     public static void main(String[] args){   
  5.         String separator = File.separator;   
  6.         String fileName = "text.txt";   
  7.         String path = "mydir1" + separator + "mydir2" + separator + "text.txt";   
  8.         //File f = new File(path, fileName);   
  9.         File f = new File(path);   
  10.         if(f.exists()){   
  11.             System.out.println("文件名:" + f.getAbsolutePath());   
  12.             System.out.println("文件大小:" + f.length());   
  13.         }else{   
  14.             //这里为什么要用getParentFile(),不能直接mkdirs() ?   
  15.             f.getParentFile().mkdirs();   
  16.             //f.mkdirs();   
  17.             try{   
  18.                 f.createNewFile();   
  19.             }catch(IOException ex){   
  20.                 ex.printStackTrace();   
  21.             }   
  22.         }   
  23.     }   
  24. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics