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文件才能做这样的操作,所以我就多试验了下。
来源:https://blog.csdn.net/qq_28259353/article/details/51456718


猜你喜欢
- 为了打造流畅的用户导航体验,我们不得不依赖智能手机最常见的一个功能:触摸。触摸改变应用程序的视图是现在最流行一种导航设计。在本文中,我们将经
- 这几天做项目,有些地方的图片需要用到圆形图片,所以百度了一下,在github上找到一个开源项目,处理很简单,效果如下:使用起来特别简单,一共
- 资源服务器就是业务服务 如用户服务,订单服务等 第三方需要到资源服务器调用接口获取资源ResourceServerConfigResourc
- 错误信息Exception in thread "main" java.lang.ClassCastException:
- 实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输
- SpringBoot2底层注解一、@ImportResource@Conditional注解,是根据条件进行装配。满足了 Condition
- service有两种类型:本地服务(Local Service):用于应用程序内部远程服务(Remote Sercie):用于android
- Kotlin基础教程之Run,标签Label,函数Function-Type在Java中可以使用{}建立一个匿名的代码块,代码块会被正常的执
- app中肯定是少不了与用户交互的各种dialog,下面给大家介绍几种提示框的提示。一般创建一个对话框需要经过以下几步:1、创建AlertDi
- 最近在做数据挖掘的课程设计,需要将数据分析的结果很直观的展现给用户,这就要用到数据统计图,要实现这个功能就需要几个第三方包了:1. 
- 为了引入这个概率 首先从需求说起 即:现有某Activity专门用于手机属性设置 那么应该如何做呢? 根据已学知识 很快一个念头闪过 即:A
- 以上是集成测试后的Jprofiler演示效果图今晚想在IDEA中集成一下JProfiler11(现在有12版本了)工具,去网上看了下都是老版
- 现在项目中有使用到音视频相关技术,在参考了网上各种大牛的资料及根据自己项目实际情况(兼容安卓6.0以上版本动态权限管理等),把声音录制及播放
- 使用simplecommand下载网络图片,并显示到ImageView控件上。1 在app module的build.gradle将simp
- 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901很多的A
- 一个简单的HelloSpringMVC程序先在web,xml中注册一个前端控制器(DispatcherServlet) <?xml v
- 一、基本使用1、准备工程和引入控件1、下载、安装FastReport这一步很简单,大家在其中文网站上下载最新版的demo版就可以了,直接安装
- springboot天生支持使用hibernate validation对参数的优雅校验,如果不使用它,只能对参数挨个进行如下方式的手工校验
- 关于Android开发可以使用的工具有eclipse和Android studio等,这两个工具都各有各的好处和不足。studio是谷歌推出
- log4j MDC实现日志追踪MDC 中包含的可以被同一线程中执行的代码所访问内容。当前线程的子线程会继承其父线程中的 MDC 的内容。记录