java文件读写工具类
依赖jar:commons-io.jar
1、写文件
// by FileUtils
List<String> lines = FileUtils.readLines(file, "UTF-8");
// by IOUtils
List<String> lines = IOUtils.readLines(new FileInputStream(file), "UTF-8");
2、读取文件
// by FileUtils
FileUtils.writeLines(file, "UTF-8", lines);
// by IOUtils
IOUtils.writeLines(lines, null, new FileOutputStream(file));
特殊需求:FileUtils/IOUtils中写入文本的方法看上去都是只能一次性的批量写入多行,并覆盖原有的文本,如果我们需要单行写入怎么办呢,
其实在IOUtils中是提供了这样的方法的,只不过比较隐晦而已:
try {
OutputStream os = new FileOutputStream(file, true);
IOUtils.writeLines(lines, null, os, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
其实就是在初始化FileOutputStream的时候 ,第二个参数append设为true就可以了。
IOUtils工具类读
List<String> lines = IOUtils.readLines(LockIP.class.getClassLoader().getResourceAsStream("warningType.data"), "UTF-8");
FileUtils工具类读写:
File config=new File("device-config.ini");
List<String> lines=new ArrayList<String>();
if(config.exists()){
try {
lines=FileUtils.readLines(config, "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lines.add(name+":"+deviceIds);
try {
FileUtils.writeLines(config, "UTF-8", lines);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
properties文件修改和读取:
/**
* 设置properties文件中的key-value
* @param fileName
* @param key
* @param value
*/
public static void setPropertiesValues(String fileName,String key, String value){
String path = CommonUtil.class.getResource("/").getPath() + fileName;
Properties props=new Properties();
FileOutputStream fos = null;
try {
props.load(CommonUtil.class.getClassLoader().getResourceAsStream(fileName));
// 修改属性值
props.setProperty(key, value);
System.out.println(path);
File file = new File(path);
if(!file.exists()){
return ;
}
// 文件输出流
fos = new FileOutputStream(file);
// 将Properties集合保存到流中
props.store(fos, "update " + key + "=" + value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fos != null){
try {
// 关闭流
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 通过key值获取properties文件中的设定值
* @param fileName
* @param key
* @return
*/
public static String getPropertiesValues(String fileName,String key){
Properties props=new Properties();
try {
props.load(CommonUtil.class.getClassLoader().getResourceAsStream(fileName));
// 获取属性值
String str = props.getProperty(key,"1");
return str;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
java文件读写工具类的更多相关文章
- java文件处理工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- android 文件读写工具类
将可以序列化的对象通过base64编码后进行保存 但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可 package com.wotlab.home.mon ...
- list集合、txt文件对比的工具类和文件读写工具类
工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...
- Java 文件切割工具类
Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...
- java文件读写操作类
借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...
- java简单的文件读写工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- properties文件读写工具类PropertiesUtil.java
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
随机推荐
- 【Android】3.19 示例19--全景图HelloWorld
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 百度全景图是一种实景地图服务.为用户提供城市.街道和其他环境的360度全景图像,用户可以通过该服务获得如临其境 ...
- ControlExtensionTest(二)-----CCControlSlider
#include "../CCControlScene.h" class CCControlSliderTest : public CCControlScene { public: ...
- 黑客编程教程(八)编写NT服务
先介绍一下什么是NT服务,实际上就是一个可以在系统启动时自动在一定身份下启动的,伴随着系统长期存在的进程. 一个NT服务有三部分构成: :Service Control Manager(SCM) 每个 ...
- hdoj1010 Temperor of the bone
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Oracle PLSQL Demo - 01.定义变量、打印信息
declare v_sal ) :; begin --if you could not see the output in console, you should set output on firs ...
- (Python mysql驱动的解决)_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h':问题的解决
在win7下安装了Python后,想安装python-MySQL,使用pip安装出现如下问题: >pip install MySQL-python _mysql.c(42) : fatal er ...
- Python3 Scrapy 安装方法
Python3 Scrapy 安装方法 (一脸辛酸泪) 写在前面 最近在学习爬虫,在熟悉了Python语言和BeautifulSoup4后打算下个爬虫框架试试. 没想到啊,这坑太深了... 看了看相关 ...
- ngApp指令,也就是ng-app属性
翻译:https://docs.angularjs.org/api/ng/directive/ngApp 使用这个指令去 自动引导 一个AngularJS 应用程序. ngApp 指令规定了html ...
- css3和jquery实现的可折叠导航菜单(适合手机网页)
之前为大家介绍了好几款css3导航,今天为大家在介绍的是一款适合放在手机网页的导航菜单.点击列表图标以下拉式的形式显示菜单,单击关闭,动画关闭.效果相当不错.效果图如下: 在线预览 源码下载 这个 ...
- nand ECC 算法记录
nandflash ECC 原理记录. nand ECC 全称是Error Checking and correction. 该算法分为列校验和行校验. 列校验有下图所示: * 如上图所示, CP0 ...