简体   繁体   English

从android中的文本文件中读取

[英]Reading from a text file in android

I'm trying to read a random line from a file. 我正在尝试从文件中读取随机行。 My code doesn't have error, just comes up with a force close as soon as it runs in the emulator and I can't work out why! 我的代码没有错误,只要在模拟器中运行就会关闭一个强制关闭,我无法解决原因!

public class filereader extends Activity {
     TextView t = (TextView)findViewById(R.id.text);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Scanner s = new Scanner(getResources().openRawResource(R.raw.lev1)); {

    try { 
        while (s.hasNext()) { 
            String word = s.next(); 
            t.setText(word); 
        } 
    } 
    finally { 
        s.close(); 
    } 
}

} }

TextView t = (TextView)findViewById(R.id.text); TextView t =(TextView)findViewById(R.id.text);

you can not run findViewById until the setContentView has been called: 在调用setContentView之前,您无法运行findViewById:

   TextView t = null;    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t = (TextView)findViewById(R.id.text);
}

please be sure that you declare text inside the main.xml 请确保在main.xml声明text

do this 做这个

    BufferedReader myReader = null;
    try 
    {
        fIn = openFileInput("customer_number.txt");         
        myReader = new BufferedReader(new InputStreamReader(fIn));
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String aDataRow = "";
    //String aBuffer = "";  
    try 
    {
        while ((aDataRow = myReader.readLine()) != null) {
            aBuffer += aDataRow + "\n";
            // TO display Whole Data of File
            Toast.makeText(getBaseContext(),aBuffer,Toast.LENGTH_SHORT).show();

     }
        // To display Last Entered Number
        Toast.makeText(getBaseContext(),last_number,Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
public void readfromfile(){
    try {
         FileInputStream fileIn=openFileInput("mytextfile.txt");
         InputStreamReader InputRead= new InputStreamReader(fileIn);
         char[] inputBuffer= new char[READ_BLOCK_SIZE];
         int charRead;

         while ((charRead=InputRead.read(inputBuffer))>0) {
             // char to string conversion
             String readstring=String.copyValueOf(inputBuffer,0,charRead);
             String s +=readstring;
         }
         InputRead.close();
         Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show();

     } catch (Exception e) {
         e.printStackTrace();
     }
}

It will read the text file named "mytext.txt" string by string and store it by appending it to string variable s. 它将按字符串读取名为“mytext.txt”的文本文件,并通过将其附加到字符串变量s来存储它。 So the variable "s" contains final string taken from file. 因此变量“s”包含从文件中获取的最终字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM