博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
保存读取文件
阅读量:2298 次
发布时间:2019-05-09

本文共 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/

你可能感兴趣的文章
Java Map遍历方式方式及性能测试(转)
查看>>
浅谈java分布式系统(转)
查看>>
leftjoinon后and和where后and区别例子
查看>>
用Collections.sort方法对list排序有两种方法(转)
查看>>
Error configuring application listener of class org.springframework.web.context.
查看>>
数据库建表注意事项(持续更新)
查看>>
Web服务实现方案(REST+SOAP+XML-RPC)简述及比较(转)
查看>>
CompletionService实例
查看>>
CompletionService实例-Mine
查看>>
ProtoStuff 序列化例子
查看>>
用mybatis的时候,实体类字段基本类型最好是包装类
查看>>
IDEA 激活网站
查看>>
SpringMVC Date类型PO,返回页面是毫秒的时间戳
查看>>
Windows平台下Git服务器搭建(转)
查看>>
creating server tcp listening socket 127.0.0.1:6379: bind No error(转)
查看>>
Redis For Windows安装及密码、IP限制
查看>>
FindBugs-IDEA插件的使用(转)
查看>>
findbugs 常见问题 及解决方案(转)
查看>>
Windows下Mysql5.7开启binlog步骤及注意事项(转)
查看>>
windows 全量增量备份和还原(转)
查看>>