当前位置: 代码迷 >> J2SE >> Properties遇到的有关问题
  详细解决方案

Properties遇到的有关问题

热度:141   发布时间:2016-04-24 01:28:47.0
Properties遇到的问题
本人java菜鸟一枚,用Properties存储数据时出现一个问题:我先用
File file=new File("test.txt");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
创建一个文件,然后用Properties存储两组数据,运行后,记事本打开test.txt文件,居然是空白。。。。。
全部代码如下


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
import java.util.Scanner;


public class Test {

/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

File file=new File("test.txt");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Please input a name:");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
System.out.println("Please input a number:");
Double num=sc.nextDouble();
Properties pro=new Properties();
pro.put("name",name);
pro.put("number", num.toString());
Writer wr=new FileWriter("test.txt");
pro.store(wr, "");//运行后test.txt是空的,如果不要前面的18-26行的代码,运行正常
}
}

}


望大虾帮帮忙

------解决方案--------------------
我表示运行正常,文件产生在工程的根目录下。。。。
Java code
Please input a name:2Please input a number:2##Sat Mar 31 20:20:54 CST 2012name=2number=2.0
------解决方案--------------------
同上

Please input a name:
asfsdfsf
Please input a number:
123123

#
#Sat Mar 31 20:17:49 CST 2012
name=asfsdfsf
number=123123.0

------解决方案--------------------
结贴给分!
  相关解决方案