本文共 4771 字,大约阅读时间需要 15 分钟。
// 保存文件 OnClickListener save_listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (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(); // 关闭FileOutputStream Toast.makeText(context, "保存文件成功", 1000).show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.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(); // 关闭FileOutputStream Toast.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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ostream != null) { try { ostream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } break; default: break; } } }; btn_save.setOnClickListener(save_listener); btn_save_sd.setOnClickListener(save_listener); // 读取 OnClickListener output_listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FileInputStream 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 block e.printStackTrace(); } } } }; btn_out.setOnClickListener(output_listener); btn_out_sd.setOnClickListener(output_listener);
转载地址:http://hfkib.baihongyu.com/