黑马程序员技术交流社区

标题: File问题 [打印本页]

作者: qmayuan    时间: 2013-8-1 18:13
标题: File问题
File file = new File("prop.ini");  file.createNewFile();
我想问下这个如果不指定盘符,默认会创建到哪里,为什么?

我自己写了段代码,用eclipse运行成功了 ,但是文件找不到了,用cmd运行会出错误~!
所有代码:
import java.util.*;
import java.io.*;
class PropertyDemo
{
        public static void main(String[] args) throws IOException
        {
                Properties p = new Properties();
                File file = new File("prop.ini");
                if(!file.exists())
                {
         file.createNewFile();
                }
                FileInputStream fis = new FileInputStream(file);
                int count = 0;
                p.load(fis);

                String value = p.getProperty("time");

                if(value!=null)
                {
                        count = Integer.parseInt(value);
                        if(count>3)
                                System.out.println("您的次数已经使用完毕,请充值");
                }
                count++;
                p.setProperty("time",count+"");
               
                FileOutputStream fos = new FileOutputStream(file);
                p.store(fos,"使用信息");
                 fis.close();
                 fos.close();

        }

}


Property1.jpg (91.04 KB, 下载次数: 52)

感觉是找不到Properties呢

感觉是找不到Properties呢

p2.jpg (28.93 KB, 下载次数: 32)

p2.jpg

作者: xuaner0719    时间: 2013-8-1 20:37
File file = new File("prop.ini");  
file.createNewFile();
eclipse默认创建到项目文件目录
如果硬要在cmd中运行不妨在这样写路径
File file = new File("./prop.ini");  
file.createNewFile();
就是当前目录下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2