首页 > 其他 > 详细

Guava入门第八章(Files)

时间:2021-08-20 21:26:37      阅读:17      评论:0      收藏:0      [点我收藏+]
package com.lvshihao.guava;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class FilesTest {

    private final String SOURCE_FILE="D:\\IDEA_PROJECT\\test\\src\\test\\java\\files\\My.txt";
    private final String TARGET_FILE="D:\\IDEA_PROJECT\\test\\src\\test\\java\\files\\Mye.txt";

    @Test
    public void testCopyFileWithGuava() throws IOException {
        Files.copy(new File(SOURCE_FILE),new File(TARGET_FILE));
        assertThat(new File(TARGET_FILE).isFile(),equalTo(true));
    }
    @Test
    public void testCopyFileWithJDKNio2() throws IOException {
        java.nio.file.Files.copy(
                Paths.get(SOURCE_FILE),
                Paths.get(TARGET_FILE),
                StandardCopyOption.COPY_ATTRIBUTES);
        assertThat(new File(TARGET_FILE).isFile(),equalTo(true));
    }

    @Test
    public void testMoveFile() throws IOException {
       try{
           Files.move(new File(SOURCE_FILE),new File(TARGET_FILE));
           assertThat(new File(SOURCE_FILE).exists(),equalTo(false));
           assertThat(new File(TARGET_FILE).exists(),equalTo(true));
       }finally {
           Files.move(new File(TARGET_FILE),new File(SOURCE_FILE));
       }
    }

    @Test
    public void testToString() throws IOException {
        final String expectedString ="lvshihao with guava hello html is ib +" +
                "please read the guava document or source code.+" +
                "the guava source code is very+";
        List<String> strings = Files.readLines(new File(SOURCE_FILE), Charsets.UTF_8);
        String result = Joiner.on("\n").join(strings);
        assertThat(result,equalTo(expectedString));
    }
}

Guava入门第八章(Files)

原文:https://www.cnblogs.com/lvshihao/p/15167650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!