google guava io

一.写字节数组

<span style="font-size:14px;">public static void fileWrite(String fileName, String contents) {
		final File newFile = new File(fileName);
		try {
			Files.write(contents.getBytes(), newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}</span>


二.读文件(一次性读完)

<span style="font-size:14px;">public static void fileRead(String fileName) {
		File testFile = new File(fileName);
		try {
			List<String> lines = Files.readLines(testFile, Charsets.UTF_8);
			for (String line : lines) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}</span>


三.读文件(逐行读取)
<span style="font-size:14px;">static class CounterLine implements LineProcessor<Integer> {
        private int rowNum = 0;
       @Override
        public boolean processLine(String line) throws IOException {
            rowNum ++;
            return true;
        }

        @Override
        public Integer getResult() {
            return rowNum;
        }
    }
	
	/**
	 * 逐行读取
	 * @param fileName
	 */
	public static void fileReadByLine(String fileName) {
		File testFile = new File(fileName);
        CounterLine counter = new CounterLine();
        try {
			Files.readLines(testFile, Charsets.UTF_8, counter);
			System.out.println(counter.getResult());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值