InputStreamReader is not reading č ž š characters

I'm working on a project, but when I am reading from file it can't read some characters (like č , ž , š, etc.) I dont know what am I am doing wrong.

Here is my code:

try {
    reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

BufferedReader br = new BufferedReader(reader);
for(int i=-1;i<position;i++){
    try {
        temp = "" + br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

try {
    reader.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Answers


Your problem is one of encoding. Files only store bytes.

There are many ways to map bytes to characters (those ways are called encoding).

When you read from a text file, you must know and specify which encoding to use.

If you don't specify the encoding in Java, the platform default encoding will be used, which may or may not be what you want.

In your case it is not what you want. To fix this, find out the correct encoding and specify it in the InputStreamReader constructor.

A common encoding to try would be UTF-8. If you told us what you see instead of those characters, we could help you guess the correct encoding.


Try this way:

try {
    reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

    BufferedReader in = new BufferedReader(reader);

    while( (s = in.readLine()) != null) {


    String UTF8Str = new String(s.getBytes(),"UTF-8"));
    temp=""+UTF8Str;

    }

Need Your Help

Convert multiple doc types (xls, doc, gif, png) to pdf in ruby

ruby-on-rails pdf

I am looking for a way to do this within a ruby on rails app. Need to do this locally within my service as the data is sensitive, I cant use external web services to do this.

Understanding the job of routes in Node.js (api) when utilizing Backbone.js (frontend)

node.js backbone.js routing routes views

I am just beginning to learn using Backbone.js. Previously I have used a web framework built on top of Node.js to handle all the routes and responses. With Backbone is the possibility of a SPA (sin...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.