将TXT文本在内存卡上保存、查看和删除

 

 

private EditText title_ed;
	private EditText content_ed;
	private Button save;
	private Button read;
	private Button delete;
	private TextView tv_title;
	private TextView tv_content;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.save_to_card);

		title_ed = (EditText) findViewById(R.id.title);
		content_ed = (EditText) findViewById(R.id.content);

		save = (Button) findViewById(R.id.button);
		read = (Button) findViewById(R.id.read);
		delete = (Button) findViewById(R.id.delete);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_content = (TextView) findViewById(R.id.tv_content);

		save.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				String title = title_ed.getText().toString();
				String content = content_ed.getText().toString();

				/*
				 * Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机
				 * 装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
				 */
				if (Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED)) {
					File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
					File saveFile = new File(sdCardDir, title + ".txt");
					// File saveFile = new File(sdCardDir.getPath() +
					// java.io.File.separator+ title + ".txt");
					FileOutputStream outStream;
					try {
						outStream = new FileOutputStream(saveFile);
						outStream.write(content.getBytes());
						outStream.close();
						Toast.makeText(SaveToCard.this, "保存完成",
								Toast.LENGTH_LONG).show();
					} catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(SaveToCard.this, "保存不成功",
								Toast.LENGTH_LONG).show();
					}
				}
			}

		});

		read.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String title = title_ed.getText().toString();
				File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
				File file = new File(sdCardDir.getPath() + 
						java.io.File.separator + title + ".txt");
				if(file.exists()){
					try {
						FileInputStream inStream = new FileInputStream(file);
						ByteArrayOutputStream outStream = new ByteArrayOutputStream();
						byte[] buffer = new byte[1024];
						int length = -1;
						while ((length = inStream.read(buffer)) != -1) {
							outStream.write(buffer, 0, length);
						}
						outStream.close();
						inStream.close();
						
						tv_title.setText(title);
						tv_content.setText(outStream.toString());
					} catch (IOException e) {
						Log.i("FileTest", e.getMessage());
					}
				}
				else{
					Toast.makeText(SaveToCard.this, "文件不存在" + sdCardDir.getPath() + 
							java.io.File.separator + title + ".txt", Toast.LENGTH_LONG)
					.show();
				}
			}

		});

		delete.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
				File saveFile = new File(sdCardDir.getPath()
						+ java.io.File.separator + title_ed.getText() + ".txt");
				if (saveFile.exists()) {
					saveFile.delete();
					Toast.makeText(SaveToCard.this, "删除成功", Toast.LENGTH_LONG)
							.show();
				} else {
					Toast.makeText(SaveToCard.this, "文件不存在", Toast.LENGTH_LONG)
							.show();
				}
			}

		});

	}
 


将TXT文本在内存卡上保存、查看和删除

你可能感兴趣的:(txt)