您的位置:首页 > 其它

Commons IO 用法举例

2016-07-23 23:30 246 查看
public class TestCommonsio {

/**
* 测试 commons-io 读取文件
* 关注 FileUtils.readLines 方法
*/
@Test
public void test01() throws IOException {
File file = new File("testCommonsIoFile.txt");
List<String> lines = FileUtils.readLines(file);
int i=0;
for(String str:lines){
i++;
System.out.println("第 " + i + " 行数据为:\t" + str);
}
}

/**
* 关注 FileUtils.writeStringToFile 方法
* @throws IOException
*/
@Test
public void test02() throws IOException {
String str = "5555555555555555555555";
String ENTER = "\n";
// \\ 表示路径
File file = new File("testio\\testWriteStringToFile.txt");
//
/**
* 每输入一行以后,加上一个换行符
*
* 参数 1:文件
* 参数 2:写入文件的字符串
* 参数 3: true 表示追加,加在后面
*/
FileUtils.writeStringToFile(file,str+ENTER,true);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  commons io