Android:将特定字符的文件读取到另一个特定字符

时间:2015-05-07 00:39:24

标签: android android-assets

我试图读取资产中的txt文件。文件的结构如下:1 [some text] 1 2 [some text] 2 3 [some text] 3 ...。 我想要做的是阅读与特定数字相对应的[some text]部分,即如果我通过数字1,我​​将从1到1读取。我已尝试过这但它没有用:

AssetManager manager = this.getAssets();
    InputStream input = null;
    InputStreamReader in = null;
    try {
        input = manager.open("facil/alexubago/songlyrics.txt");
        in = new InputStreamReader(input);
    } catch (IOException e1) {
        Log.d("ERROR DETECTED", "ERROR WHILE TRYING TO OPEN FILE");
    }
    try {
        char current;
        char current2;
        String themessage ="" ;
        while (in.ready()) {

            final Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roland.ttf");
            final TextView extField = (TextView) findViewById(R.id.thelyrics);
            extField.setTypeface(tf);
            current = (char) in.read();
            int a = 1;
            char b = (char) a;
            if(current == b){
                    current2 = (char) in.read();
                while(current2 != b){
                    themessage = themessage+current2;}
                extField.setText(themessage);
                Log.d("caracter", ""+current);}
                Toast.makeText(lyricsfreakgame.this,themessage, Toast.LENGTH_SHORT).show();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

int a = 1;
char b = (char) a;

这不会将1转换为char'1'。 b的值为1而不是'1'。你应该这样做

char b = '1';