java读写ini文件、FileOutputStream问题

作者:老李与GPT 时间:2023-11-29 08:50:13 

java读写ini文件、FileOutputStream

在上课让学生练习文件读写,就让他们做了一个使用文件保存账号和密码的练习,有一个比较爱学的学生就用到了了.ini文件,也就是我们所说的配置文件来保存账号密码(学生很聪明,知道用键值对的方式保存密码,作为老师的我很汗颜啊!!!),我没仔细想就说你是不是进行了多次字符串分割来获取信息的,他说不是,只需要加几步代码就可以了。当时没细想说做出来就好……

后来我越想越不对,就回办公室自己写了一下,发现还是有很多问题的。

在查看文档的情况下,知道要读取类似于键值对的文件,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文件才能做这样的操作,所以我就多试验了下。

java读写ini文件、FileOutputStream问题

来源:https://blog.csdn.net/qq_28259353/article/details/51456718

标签:java,ini文件,FileOutputStream
0
投稿

猜你喜欢

  • Java实现的3des加密解密工具类示例

    2023-08-21 14:00:01
  • C# XML基础入门小结(XML文件内容增删改查清)

    2022-10-18 17:12:02
  • 浅谈@FeignClient中name和value属性的区别

    2023-11-06 13:04:14
  • C#及WPF获取本机所有字体和颜色的方法

    2022-12-23 01:23:13
  • 详解如何在Spring Security中自定义权限表达式

    2023-11-08 05:11:24
  • Java动态 代理的应用详解

    2023-11-25 08:15:24
  • Android ViewDragHelper完全解析 自定义ViewGroup神器

    2023-04-19 05:56:19
  • Java中两种基本的输入方式小结

    2023-02-06 18:37:24
  • Java进程cpu占用过高问题解决

    2021-08-09 00:16:59
  • C# 格式化字符首字母大写的方法

    2022-04-28 00:05:12
  • Unity实现枚举类型中文显示

    2023-02-22 12:00:28
  • Java Collection集合的三种遍历方式详解

    2023-04-29 08:11:40
  • java自带的MessageDigest实现文本的md5加密算法

    2023-10-08 03:35:29
  • 详解如何在Java中加密和解密zip文件

    2022-12-07 12:26:46
  • C#预处理器指令的用法实例分析

    2023-03-09 16:21:07
  • Android6.0 消息机制原理解析

    2023-08-06 12:19:44
  • 使用Springboot对配置文件中的敏感信息加密

    2021-05-24 10:50:25
  • springboot 高版本后继续使用log4j的完美解决方法

    2021-12-06 11:12:41
  • Java Thread之Sleep()使用方法及总结

    2023-11-16 10:38:35
  • Android应用程序转到后台并回到前台判断方法

    2022-11-12 19:49:35
  • asp之家 软件编程 m.aspxhome.com