【问题标题】:Android : read a file from a specific character to another specific characterAndroid:从特定字符读取文件到另一个特定字符
【发布时间】:2015-05-07 00:39:23
【问题描述】:

我正在尝试读取资产中的 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();
    }

【问题讨论】:

    标签: android android-assets


    【解决方案1】:
    int a = 1;
    char b = (char) a;
    

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

    char b = '1';
    

    【讨论】:

    • int a = 1 只是为了测试它给出的数字不是这样给出的
    • 使用 int a=1,您正在尝试读取 1 个块内的文本(1 [text] 1),对吧?
    猜你喜欢
    • 2023-03-23
    • 2014-12-04
    • 2011-10-29
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多