我是 Android 编程新手,这段代码给出了 FilenotFound 异常,并通过 catch 块,我应该在哪里保存我的 .txt 文件。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myCOde=findViewById(R.id.code);
String filename="bully rap.txt";
try {
readCode(filename);
} catch (Exception e) {
myCOde.setText("file not found");
}
}
private void readCode(String filename) throws Exception{
File filesDir=MainActivity.this.getFilesDir();
File myFile=new File(filesDir,filename);
BufferedReader br = new BufferedReader(new FileReader(myFile));
String st;
StringBuilder code= new StringBuilder("hello");
while ((st = br.readLine()) != null){
code.append(st);
myCOde.setText(code);
}
慕斯709654
相关分类