How can I read a text file in Android?

前端 未结 7 1552
-上瘾入骨i
-上瘾入骨i 2020-11-22 07:51

I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the catch block). I put the text file in the applicati

7条回答
  •  星月不相逢
    2020-11-22 08:36

    Try this

    try {
            reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          String line="";
          String s ="";
       try 
       {
           line = reader.readLine();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
          while (line != null) 
          {
           s = s + line;
           s =s+"\n";
           try 
           {
               line = reader.readLine();
           } 
           catch (IOException e) 
           {
               e.printStackTrace();
           }
        }
        tv.setText(""+s);
      }
    

提交回复
热议问题