嘿伙计我想创建一个txt。我的appfolder中的内部目录中的文件... 我写了这段代码......但他没有工作......我希望有人能帮助我。
private void writeFile() {
String message = "You have did it!";
String file_name = "test_file";
try{
FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
fileOutputStream.write(message.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Message saved.", Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
private void readFile() {
try{
String message;
FileInputStream fileInputStream = openFileInput("test_file");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
message = bufferedReader.readLine();
usernameField.setText(message);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}