【发布时间】:2016-05-23 15:13:34
【问题描述】:
所以我试图将从套接字接收的数据写入文本文件,然后读取这些数据。
我的 MainActivity 中有这 2 种方法(只是测试看看读写文件的工作原理):
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”保存在哪里???
【问题讨论】:
-
我很确定它依赖于上下文,保存到其当前设置的文件目录。 stackoverflow.com/questions/4926027/…
标签: android