java读写ini文件、FileOutputStream问题怎么解决
java读写ini文件、FileOutputStream在查看文档的情况下,知道要读取类似于键值对的文件,java已经给我提供了一个叫Properties的类,简单如下:Filefile=newFile("../JavaPractice/a.txt");try{FileInputStreaminputStream=newFileInputStream(file);Propertiesproperties=newProperties();properties.load(inputStream);//把文件中
java读写ini文件、FileOutputStream
在查看文档的情况下,知道要读取类似于键值对的文件,java已经给我提供了一个叫 Properties的类,简单如下:
File file = new File("../JavaPractice/a.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(inputStream);//把文件中的内容放在流里面
properties.list(System.out);//通过这个方法把内容打印出来
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}打印如下:
-- listing properties --
a=b
12=222
asdasd1=asd111122
12123=123222
如果要直接使用键得到值得代码如下:
File file = new File("../JavaPractice/a.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(inputStream);//把文件中的内容放在流里面
// properties.list(System.out);//通过这个方法把内容打印出来
System.out.println(properties.get("a"));//通过get方法得到对应值
System.out.println(properties.get("0"));//如果没有对应的值就为null
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}打印结果:
b
null
使用properties.set()方法存值
File file = new File("../JavaPractice/a.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(inputStream);//把文件中的内容放在流里面
// properties.list(System.out);//通过这个方法把内容打印出来
System.out.println(properties.get("a"));//通过get方法得到对应值
System.out.println(properties.get("0"));//如果没有对应的值就为null
properties.setProperty("java", "love java");//使用此方法存值
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}我以为就这样就存进去,满怀欣喜的打开文件,发现并没有真正的存入值,但是我又试着使用properties.get()方法,却能得到我放进去的值,很是疑惑,只得再次看api。
File file = new File("../JavaPractice/a.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(inputStream);//把文件中的内容放在流里面
// properties.list(System.out);//通过这个方法把内容打印出来
System.out.println(properties.get("a"));//通过get方法得到对应值
System.out.println(properties.get("0"));//如果没有对应的值就为null
properties.setProperty("java", "love java");//使用此方法存值
FileOutputStream outputStream = new FileOutputStream(file);
properties.store(outputStream, null);//只有调用这个方法才能写进文件,因为前面set方法只是写在流里面,直到调用这个方法才真正的写入文件里。
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}new FileOutputStream 的位置有关系吗?
如果把FileOutputStream outputStream = new FileOutputStream(file);放在load前面,那么文件里面所有的信息都会被覆盖,放在load后面就不会了,因为load后就保存在一个流里面。
因为FileOutputStream outputStream = new FileOutputStream(file);就默认覆盖文件,直接是文件为空,除非FileOutputStream outputStream = new FileOutputStream(file,true);,所有有的时候初学者会把这些东西搞混淆,最后怎么出错也不知道。(汗颜!!!我也出过这个错误……)
最后细心的朋友可能会发现我并不是.ini文件,对的,我发现并不是只有ini文件才能做这样的操作,所以我就多试验了下。

Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















