这只是一个简单的程序来创建各种日记。从文件中读取文本垃圾值(疑似)存在于录制的文本之前。没有任何一个toasts显示任何垃圾。请帮忙
我在阅读本课时遇到错误
public class ReadDiary extends AppCompatActivity {
TextView textView;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_diary);
Intent intent=getIntent();
String filename= intent.getStringExtra("example.mydiary");
file=new File(this.getFilesDir(),filename);
String data="";
try {
BufferedReader r = new BufferedReader(new FileReader(file));
int ch;
while ((ch = r.read()) != -1)
{
data += (char) ch;
}r.close();
}catch (Exception e)
Toast.makeText(this,data,Toast.LENGTH_LONG);
textView=(TextView)findViewById(R.id.textView);
// textView.setMovementMethod(new ScrollingMovementMethod());
textView.setText(" ");
textView.setText(""+data);
}
}
写入文件的类是
public class DiaryEntry extends AppCompatActivity {
File file;
EditText editText;
String filename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary_entry);
Intent intent= getIntent();
filename=intent.getStringExtra("example.mydiary");
file=new File(filename);
if(!file.exists())
try {
file.createNewFile();
Toast.makeText(this,"Saved....",Toast.LENGTH_LONG).show();
} catch (IOException e) {
// e.printStackTrace();
}
editText=(EditText)findViewById(R.id.Diaryentry);
editText.setText("");
Toast.makeText(DiaryEntry.this,"Saved",Toast.LENGTH_LONG).show();
}
protected void saveData(View view)
{try {
Toast.makeText(DiaryEntry.this,"Really?",Toast.LENGTH_LONG).show();
String s="";
s+=editText.getText().toString();
s+="";
Writer writer = new OutputStreamWriter(new ObjectOutputStream(openFileOutput(filename, Context.MODE_PRIVATE)));
writer.flush();
writer.write(s);
writer.flush();
writer.close();
Toast.makeText(DiaryEntry.this,editText.getText().toString(),Toast.LENGTH_LONG).show();
Intent intent= new Intent(this,DiaryHomePage.class);
Toast.makeText(DiaryEntry.this,s,Toast.LENGTH_LONG).show();
startActivity(intent);
}
catch (Exception e)
{
}
}
}