所以即時嘗試寫入我從套接字到文本文件,然後讀取這些數據的數據。Android Studio從.txt文件讀取/寫入,保存路徑?
我有這2種方法在我的MainActivity(只是測試,看看如何讀取和/寫入到文件中的作品):
public void WriteBtn() {
// add-write text into file
try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write("Test");
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void ReadBtn() {
//reading text from file
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[256];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
我打電話給他們的按鈕,但我想知道,在那裏它保存我的「mytextfile.txt」?
我很確定它是依賴於上下文,保存到它的當前設置文件目錄。 http://stackoverflow.com/questions/4926027/what-file-system-path-is-used-by-androids-context-openfileoutput – zgc7009