guava-IO使用

2019-04-16 12:41:16 浏览数 (1)

代码语言:javascript复制
@Test
	public void test1() throws IOException {
		// Read the lines of a UTF-8 text file
		File file = new File("F:/prd/kw/olay1.txt");
		ImmutableList<String> lines = Files.asCharSource(file, Charsets.UTF_8).readLines();
		for (String line : lines) {
			System.out.println(line);
		}
	}

	@Test
	public void test2() throws IOException {
		// Count distinct word occurrences in a file
		Multiset<String> wordOccurrences = HashMultiset.create(Splitter.on(CharMatcher.WHITESPACE).trimResults().omitEmptyStrings().split(Files.asCharSource(new File("F:/prd/kw/test.txt"), Charsets.UTF_8).read()));
		System.out.println(wordOccurrences);
	}

	@Test
	public void test3() throws IOException {
		File file = new File("F:/prd/kw/olay1.txt");
		HashCode hash = Files.asByteSource(file).hash(Hashing.sha1());
		System.out.println(hash);
	}

	@Test
	public void test4() throws MalformedURLException, IOException {
		File file = new File("F:/prd/kw/ok.txt");
		// Copy the data from a URL to a file
		Resources.asByteSource(new URL("http://ifeve.com/google-guava-io/")).copyTo(Files.asByteSink(file));
	}

0 人点赞