依赖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文件读写工具类的更多相关文章

  1. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  2. properties文件读写工具类

    java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...

  3. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  4. android 文件读写工具类

    将可以序列化的对象通过base64编码后进行保存 但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可 package com.wotlab.home.mon ...

  5. list集合、txt文件对比的工具类和文件读写工具类

    工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...

  6. Java 文件切割工具类

    Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...

  7. java文件读写操作类

    借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...

  8. java简单的文件读写工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  9. properties文件读写工具类PropertiesUtil.java

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

随机推荐

  1. Unity刚体穿透问题测试以及解决

    测试环境很简单,一面墙,红色方块不停向前 然后,由于刚体是FixedUpdate执行的,把FixedUpdate执行间隔调慢一些方便Debug: OK,下面还原一次经典的穿透问题: 测试脚本: voi ...

  2. RhinoMock学习-绑定回调

    Expect.Call(testClass.Test(new Arg())) .IgnoreArguments() .Return() .Callback(); return true; })); . ...

  3. 批处理学习笔记11 - del命令和rd命令

    这两个命令都是删除,所以放一块说了 del 删除文件 rd 删除目录(文件夹) ------------------------------------------------------------ ...

  4. Transport level information does not match with SOAP Message namespace URI错误的理解

    理论知识: 转自:http://wso2.com/library/articles/differentiating-between-soap-versions-looking-soap-message ...

  5. Cocos2d-x和时间有关的代码

    用cocos2d-x获取系统时间,格式为年月日时分秒: void GetTime(float dt) { struct tm *tm; #if (CC_TARGET_PLATFORM == CC_PL ...

  6. Oracle PLSQL Demo - 09.Open、Fetch遍历游标[Open, Fetch, Close Record CURSOR]

    declare r_emp scott.emp%rowtype; cursor cur_emp is select t.* from scott.emp t; begin open cur_emp; ...

  7. WCF寄宿到Windows Service

    WCF寄宿到Windows Service[1] 2014-06-14 WCF寄宿到Windows Service参考 WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序 ...

  8. spring使用ApplicationContext读取资源文件

    @Autowired private ApplicationContext applicationContext; Resource resource = applicationContext.get ...

  9. jq 跳转方式汇总

    按钮式: <INPUT name="pclog" type="button" value="GO" onClick="loc ...

  10. hdu2199(高精度二分模版)

    Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and ...