本人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行的代码,运行正常
}
}}
望大虾帮帮忙