保存读取文件

来源:互联网 发布:sql server2000 编辑:程序博客网 时间:2024/05/21 11:25
// 保存文件OnClickListener save_listener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (et_input.getText().toString().equals("")) {Toast.makeText(context, "请输入文本!", 1000).show();return;}switch (v.getId()) {case R.id.In_save:FileOutputStream fos = null;try {fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);// 以私有模式创建文件String text = et_input.getText().toString();fos.write(text.getBytes());// 写入数据fos.flush(); // 将缓冲区剩余数据写入文件fos.close(); // 关闭FileOutputStreamToast.makeText(context, "保存文件成功", 1000).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}break;case R.id.In_save_sd:if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {FileOutputStream ostream = null;try {//*File myfile = new File(Environment.getExternalStorageDirectory().getPath(),FILENAME);Log.i("In_save_sd",Environment.getExternalStorageDirectory().getPath());/*if (!myfile.exists()) {myfile.createNewFile();}*/if (myfile.exists()) {myfile.delete();}myfile.createNewFile();//*//*FileOutputStream fos = null;fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);// 以私有模式创建文件String text = et_input.getText().toString();fos.write(text.getBytes());// 写入数据fos.flush(); // 将缓冲区剩余数据写入文件fos.close(); // 关闭FileOutputStreamToast.makeText(context, "保存文件成功", 1000).show(); * *//*ostream = openFileOutput(Environment.getExternalStorageDirectory().getPath()+FILENAME, Context.MODE_WORLD_WRITEABLE);// 以私有模式创建文件*/ostream = new FileOutputStream(myfile, true);String text = et_input.getText().toString();ostream.write(text.getBytes());////ostream.flush(); // 将缓冲区剩余数据写入文件//ostream.close(); // 关闭FileOutputStream//Toast.makeText(context, "保存文件到sd卡成功", 1000).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (ostream != null) {try {ostream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}break;default:break;}}};btn_save.setOnClickListener(save_listener);btn_save_sd.setOnClickListener(save_listener);// 读取OnClickListener output_listener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubFileInputStream inStream = null;ByteArrayOutputStream stream = null;try {switch (v.getId()) {case R.id.out_put:inStream = openFileInput(FILENAME);break;case R.id.out_put_sd:if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {File sd_file = new File(Environment.getExternalStorageDirectory().getPath(),FILENAME);Log.i("out_put_sd",Environment.getExternalStorageDirectory().getPath());if (!sd_file.exists()) {Toast.makeText(context, "未找到该文件", 1000).show();return;}inStream = new FileInputStream(sd_file);}default:break;}stream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = -1;while ((length = inStream.read(buffer)) != -1) {stream.write(buffer, 0, length);}stream.close();inStream.close();et_output.setText(stream.toString());Toast.makeText(context, "获取文本成功!", 1000).show();} catch (FileNotFoundException e) {e.printStackTrace();Toast.makeText(context, "文件未找到", 1000).show();System.out.println(e.toString());} catch (IOException e) {System.out.println(e.toString());return;} finally {try {if (stream != null)stream.close();if (inStream != null)inStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}};btn_out.setOnClickListener(output_listener);btn_out_sd.setOnClickListener(output_listener);