From b394dad562c8b5bff95eb7d16c47d3a1ef8e8a90 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 16 Aug 2019 11:59:51 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fanruan/api/util/IOKit.java | 6 +++--- src/test/java/com/fanruan/api/util/IOKitTest.java | 7 +++++++ src/test/resources/com/fanruan/api/util/iokit.txt | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/com/fanruan/api/util/iokit.txt diff --git a/src/main/java/com/fanruan/api/util/IOKit.java b/src/main/java/com/fanruan/api/util/IOKit.java index 3ec1f3d..9ee5efa 100644 --- a/src/main/java/com/fanruan/api/util/IOKit.java +++ b/src/main/java/com/fanruan/api/util/IOKit.java @@ -13,7 +13,7 @@ import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.channels.Channel; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; /** * @author richie @@ -108,7 +108,7 @@ public class IOKit { * @return 读取出来的字符串 * @throws UnsupportedEncodingException 编码类型不支持则抛出此异常 */ - public static String inputStream2String(InputStream is, StandardCharsets charset) + public static String inputStream2String(InputStream is, Charset charset) throws UnsupportedEncodingException { return CommonIOUtils.inputStream2String(is, charset.toString()); } @@ -196,7 +196,7 @@ public class IOKit { * @param encode 读取资源所用的编码 * @return 表示资源内容的字符串 */ - public static String readResourceAsString(String path, StandardCharsets encode) { + public static String readResourceAsString(String path, Charset encode) { return IOUtils.readResourceAsString(path, encode.toString()); } diff --git a/src/test/java/com/fanruan/api/util/IOKitTest.java b/src/test/java/com/fanruan/api/util/IOKitTest.java index b426b68..e9922fc 100644 --- a/src/test/java/com/fanruan/api/util/IOKitTest.java +++ b/src/test/java/com/fanruan/api/util/IOKitTest.java @@ -3,6 +3,7 @@ package com.fanruan.api.util; import com.fanruan.api.Prepare; import org.junit.Assert; import org.junit.Test; +import sun.nio.cs.ext.GBK; import java.nio.charset.StandardCharsets; @@ -17,4 +18,10 @@ public class IOKitTest extends Prepare { public void testEncode() { Assert.assertEquals("UTF-8", StandardCharsets.UTF_8.toString()); } + + @Test + public void testReadResourceAsString() { + String text = IOKit.readResourceAsString("/com/fanruan/api/util/iokit.txt"); + Assert.assertEquals("Hello World!", text); + } } \ No newline at end of file diff --git a/src/test/resources/com/fanruan/api/util/iokit.txt b/src/test/resources/com/fanruan/api/util/iokit.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/src/test/resources/com/fanruan/api/util/iokit.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file