基本的なテキストファイル保存プログラムを作成しました。それは完全に動作しますが、作成されたテキストファイルがどこに格納されているのか分かりません。誰が彼らがどこに保管されているか考えていますか?テキストファイルはどこに保存されていますか?
すべての必要なアクセス許可が与えられています。
public class MainActivity extends AppCompatActivity {
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final EditText enter = ((EditText) findViewById(R.id.editText));
final TextView show = ((TextView) findViewById(R.id.textView));
Button b = ((Button) findViewById(R.id.button));
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String s = enter.getText().toString();
writeToFile(s, s);
show.setText(readFromFile(s));
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void writeToFile(String name, String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(name + ".txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String readFromFile(String name) {
String ret = "";
try {
InputStream inputStream = openFileInput(name + ".txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
public static void Save(File file, String[] data) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
try {
for (int i = 0; i < data.length; i++) {
fos.write(data[i].getBytes());
if (i < data.length - 1) {
fos.write("\n".getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
なぜあなたはアンドロイドのドキュメントでそれを見ていないのですか? – m02ph3u5