Weird Characters Displayed on Build, but not on Debug

时间:2015-10-30 21:50:42

标签: java string url web

I'm developing a Java Application using Netbeans, but while I run my app in Debug, or plain running it from Netbeans, my screen looks like so: Good. Yet, when I try to run from the Built jar in the dist folder, it looks like so: Bad.

Here is the method in which I am using to receive the content. The method is intended to get the source of text file from the web.

public static ArrayList<String> getUrlSource(String urlF) throws IOException {
    URL url = new URL(urlF);
    Scanner s = new Scanner(url.openStream());
    ArrayList<String> fileLines = new ArrayList<>();
    while (s.hasNextLine())
    {
        fileLines.add(s.nextLine());
    }
    return fileLines;
}

2 个答案:

答案 0 :(得分:2)

You're looking at the UTF-8 Byte Order Marker

The UTF-8 representation of the BOM is the byte sequence 0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text as ISO-8859-1 or CP1252 will display the characters  for this.

答案 1 :(得分:1)

The default charset may be different depending on how you launch the application. Try specifying the charset explicitly :

Scanner s = new Scanner(url.openStream(), "UTF-8");