//读取文件转换成字符串
String s=FileUtils.readFileToString(new File("D:\\d\\write.txt"),"UTF-8");
//读取文件转换成字节数组
byte[] datas=FileUtils.readFileToByteArray(new File("D:\\d\\write.txt"));
//逐行读取,返回类型是List
List list=FileUtils.readLines(new File("D:\\d\\write.txt"),"UTF-8");
//类似迭代器逐行读取,读取文件
LineIterator it=FileUtils.lineIterator(new File("D:\\d\\write.txt"),"UTF-8");
while(it.hasNext())
{
System.out.println(it.next());
}
//写出文件,指定文件对象,写入数据,指定字符集,是否开启追加模式
FileUtils.write(new File("D:/d/tt"),"啊哈哈哈" ,"UTF-8",true);
FileUtils.writeStringToFile(new File("D:/d/tt"),"啦啦啦","UTF-8",true);
//两种方法效果一样
//操作字节数组
FileUtils.writeByteArrayToFile(new File("D:/d/tt"), "wulala".getBytes("UTF-8"),true);
//写出列表,第三个参数是列表元素之间的连接字符串
List lists=new ArrayList();
lists.add("蚂蚁");
lists.add("啊哈哈的");
lists.add("lawd");
FileUtils.writeLines(new File("D:/d/tt"),lists,"--",true);
本文名称:java_io_commons读取、写出文件
标题链接:
http://kswsj.cn/article/jhdpjd.html