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"));//逐行读取,返回类型是ListList<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);
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。