博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件读写工具类
阅读量:7236 次
发布时间:2019-06-29

本文共 2419 字,大约阅读时间需要 8 分钟。

依赖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 "";
}
}

转载地址:http://ndlfm.baihongyu.com/

你可能感兴趣的文章
Objective-C中的Block回调模式
查看>>
Linux 搭建SVN server
查看>>
strcpy_s与strcpy的比較
查看>>
fscanf()函数具体解释
查看>>
JS魔法堂:关于元素位置和鼠标位置的属性
查看>>
jquery.validate+jquery.form提交的三种方式
查看>>
分形之树(Tree)
查看>>
[HTML/CSS]盒子模型,块级元素和行内元素
查看>>
HDU1698_Just a Hook(线段树/成段更新)
查看>>
学习selenium所须要具备的技术
查看>>
shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟
查看>>
高质量c c++编程
查看>>
[詹兴致矩阵论习题参考解答]习题4.1
查看>>
SQL Server 索引和表体系结构(非聚集索引)
查看>>
JS或AS中处理ARGB、RGBA颜色值时要小心
查看>>
【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
查看>>
English Metric Units and Open XML
查看>>
[ES6] 01. Intro to ES6 and traceur compiler
查看>>
专业版Unity技巧分享:使用定制资源配置文件
查看>>
【插件开发】—— 12 GEF入门
查看>>