我要保存一些东西。
如果某个目录下没有就保存在exception.txt里面,
如果这个文件大于20M就保存到exception1.txt里面
这要怎么实现呀,

解决方案 »

  1.   

    很简单,用file类阿
    说一下方法吧1)生成一个file类的对象
    File file = new File(路径名/文件名);
    2)判断文件是否存在
    file.exists() ;
    3)如果上面的方法返回true,说明文件存在
    则再判断文件的大小
    file.length() 
    这个方法返回long型数据,和你的20M去比较
    如果大于20M
    那末就新建一个文件拉
    File newfile1 = new File("exception1.txt");4)如果2)的方法返回false
    则说明文件不存在
    那末
    File newfile2 = new File("exception.txt");
      

  2.   

    因为偶现在没有java编译环境
    所以没有办法给楼主代码不过,我觉得上面写的也和代码没什莫区别了吧
      

  3.   


     File file = new File("D:\\exception.txt");
       if(file.exists()){
       long d = file.length()/1024/1024;
       if(d>20.0){
       File newfile1 = new File("exception1.txt");
       ......写入操作
       }
       else{
       ......写入操作
       }
       }
       else{
       file.createNewFile();
       }
       }