本人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行的代码,运行正常
}
}}
望大虾帮帮忙
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行的代码,运行正常
}
}}
望大虾帮帮忙
解决方案 »
- JPanel被set不可见后又设为可见,构造方法中的键盘监听还能监听吗?
- 使用synth遇到的问题
- 线程中run()怎么不运行?
- Runtime.getRuntime().exec()如何执行多条命令?
- 内存分配问题。。。。。
- 邮件主要内容接受问题
- JOptionPane.showInputDialog 能否实现未输入字符时无法点击确定
- 求助:用CallableStatement执行存储过程,程序僵死情况发生如何快速捕获异常
- 有哪位大侠知道Hashtable与Hashmap的不同吗?
- 关于邮件的主题的问题,高手们请进来
- 包引用出错了
- 求高手解决关于将Jpanel放到JScrollPanel滑块不出现,下面是我的代码
2
Please input a number:
2#
#Sat Mar 31 20:20:54 CST 2012
name=2
number=2.0
asfsdfsf
Please input a number:
123123#
#Sat Mar 31 20:17:49 CST 2012
name=asfsdfsf
number=123123.0