I have a problem with encoding of the forward slash character in Java. I have this program to illustrate what is happening -
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"
}
}
The program reads from the file "hello.txt". The content of the file is just -
http:\u002f\u002fgoogle.com
Note that this is the same as the string 'input'.
Can anyone explain to me why there is a difference in the outputs?