public class CreateFile extends Activity {
private BufferedReader reader;
private TextView tvFileReader;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvFileReader = (TextView) findViewById(R.id.tvFileReader);
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/hadatabase/";
File fileDir = new File(path);
// 如果目录不存在就新建目录
if (!fileDir.exists()) {
if (!fileDir.mkdirs()) {
Log.i("创建目录", "创建目录失败,请检查是否有sdcard读写权限。");
}
}
// 获取 textview 内容
String str = tvFileReader.getText().toString().trim();
try {
FileWriter fw = new FileWriter(path + "bb.txt");
fw.flush();
fw.write(str); // 把textview 内容写入到bb.txt文件
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
File readerFile = new File(path + "/bb.txt");
try {
reader = new BufferedReader(new FileReader(readerFile));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
// 把bb.txt文件里的内容添加到 TextView 里
tvFileReader.append(tempString);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}