java _io_commons读取、写出文件
作者:互联网
//读取文件转换成字符串
String s=FileUtils.readFileToString(new File("D:\\d\\write.txt"),"UTF-8");
//读取文件转换成字节数组
byte[] datas=FileUtils.readFileToByteArray(new File("D:\\d\\write.txt"));
//逐行读取,返回类型是List
List<String> 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<String> lists=new ArrayList<String>();
lists.add("蚂蚁");
lists.add("啊哈哈的");
lists.add("lawd");
FileUtils.writeLines(new File("D:/d/tt"),lists,"--",true);
标签:java,io,UTF,commons,lists,write,File,FileUtils,new 来源: https://blog.51cto.com/14437184/2426297