public class intro extends AppCompatActivity {
private StringBuilder text = new StringBuilder();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("assets/Introduction.txt")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('\n');
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!", Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
TextView output= (TextView) findViewById(R.id.intro);
output.setText((CharSequence) text);
}
}
[This is my project explorer][1]}
我尝试了许多用于打开资源文件夹的路径,但它显示了filenotfound异常。在这里,我在Assets文件夹中创建了Introduction.txt文件,并在其上写了一些内容。但我无法通过提供路径或文件名来打开它。
答案 0 :(得分:1)
从assets/
移除"assets/Introduction.txt"
。提供给open()
的路径是 assets/
内的相对路径。