我做了一个基本保存到文本文件程序。它工作得很好,但我找不到创建的文本文件的存储位置。有没有人知道它们存储在哪里?
已授予所有必要的权限。
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();
}
}
}
}
答案 0 :(得分:0)
该文件正在写入internal storage。这是您的应用专用的区域。除了root设备外,其他应用程序或桌面程序无法查看它。
答案 1 :(得分:0)
您可以使用以下命令 ActivityName.this.getFileDir()。getAbsolutePath()
答案 2 :(得分:0)
您可以使用context.getFileStreamPath()
返回保存文件位置的路径。根据{{3}}:
public abstract File getFileStreamPath (String name)
返回文件系统所在的绝对路径 存储使用openFileOutput(String,int)创建的文件。
答案 3 :(得分:0)
如果您在模拟器上测试此代码,则可以将shell封装到模拟器中,并可能在此目录中找到您的文件
/data/data/<appid>/files
其中<appid>
是build.gradle中指定的应用程序ID