DarkFenix
DarkFenix

Reputation: 746

Get data from text in UTF8 format URL

There is a text hosted on a server, in plain text but I try to get that data in UTF-8 format that is able to recognize "ñ" and more. I tried this

String readLine;
    try {
        URL url = new URL("http://miurl/data.txt");
        BufferedReader in = null;
        in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));

        while ((readLine = in.readLine()) != null) {
           byte ptext[] = readLine.getBytes(ISO_8859_1); 
           String value = new String(ptext, UTF_8); 
           System.out.println(value);
        }

    }
    catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

The problem is that it reads data from url but not encoded as utf8, the letters such as "ñ" are printed strangely.

Upvotes: 0

Views: 250

Answers (2)

Amit Malik
Amit Malik

Reputation: 194

I also had the same issue while reading some special characters from CSV file. So I used below code to read the files

BufferedReader bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(userCsv), "ISO-8859-1"));

In your case you may try

URL url = new URL("http://miurl/data.txt");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "ISO-8859-1"));

Upvotes: 3

Shafin Mahmud
Shafin Mahmud

Reputation: 4081

Did you simply tried this ?

        String readLine;
        try {
            URL url = new URL("http://miurl/data.txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

            while ((readLine = in.readLine()) != null) {
                System.out.println(readline);
            }

        }
        catch (IOException ex) {
            ex.printStackTrace();
        }

I tried this on this sample text from here and it parses all UTF-8 characters quite okay.

Upvotes: 2

Related Questions