【发布时间】:2015-11-19 09:41:15
【问题描述】:
我在 Java 中对正斜杠字符进行编码时遇到问题。我有这个程序来说明正在发生的事情 -
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
public class SlashTester {
public static void main(String[] args) throws FileNotFoundException, IOException {
String input = "http:\u002f\u002fgoogle.com";
System.out.println(input); // Prints "http://google.com"
String input2 = IOUtils.toString(new FileInputStream("hello.txt"), Charset.forName("UTF-8"));
System.out.println(input2); //Prints "http:\u002f\u002fgoogle.com"
}
}
程序从文件“hello.txt”中读取。该文件的内容只是 -
http:\u002f\u002fgoogle.com
请注意,这与字符串 'input' 相同。
谁能向我解释为什么输出会有所不同?
【问题讨论】:
标签: java string utf-8 character-encoding