我一直在尝试保存基本文本文件,然后从文本文件中读取作业。老师没有反应,所以没有意义问他,但我认为你们可能知道一个解决方案。
这是我撰写的源代码:
public class MainActivity extends ActionBarActivity {
final String fileName = "StorageFile.txt";
FileOutputStream outputStream;
FileInputStream inputStream;
public void saveTheFile(View view)
{
try
{
EditText textToSave = (EditText) findViewById(R.id.saveText);
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(textToSave.toString().getBytes());
outputStream.close();
Context context = getApplicationContext();
CharSequence text = "File Saved!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void readTheFile(View view)
{
try
{
InputStream instream = openFileInput(fileName);
if(instream != null)
{
InputStreamReader inputReader = new InputStreamReader(instream);
BufferedReader buffReader = new BufferedReader(inputReader);
String text;
// This part is for reading multiple lines, just in case.
do
{
text = buffReader.readLine();
} while(text != null);
EditText readText = (EditText)findViewById(R.id.readText);
readText.setText(text, TextView.BufferType.EDITABLE);
}
instream.close();
Context context = getApplicationContext();
CharSequence text = "File Read!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sets the text on the buttons programatically, since no @string was
// referenced in the XML file.
Button buttonSave = (Button) findViewById(R.id.saveFile);
buttonSave.setText("Save File");
Button buttonRead = (Button) findViewById(R.id.readFile);
buttonRead.setText("Read File");
}`
这是XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.androidui.MainActivity" >
<EditText
android:id="@+id/saveText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/saveFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@+id/saveText"
android:minHeight="36dip"
android:onClick="saveTheFile" />
<Button
android:id="@+id/readFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@+id/saveFile"
android:minHeight="36dip"
android:onClick="readTheFile" />
<EditText
android:id="@+id/readText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/readFile"
android:inputType="text" />
</RelativeLayout>
我真的迷失在这里。我无法找到任何已保存在我的任何驱动器上的任何文件夹中的文件。从初学者的角度来看,这是非常令人沮丧的。