bo.android应用系列:文件创建

目前功能:创建一个文件,并写入内容
========================
截图:

 bo.android应用系列:文件创建

 

 

 bo.android应用系列:文件创建

 

 

 

 bo.android应用系列:文件创建

 

 

========================

部分代码:

/**
 * <p>
 * 业务层<br/>
 * 2012-02-09
 * </p>
 * @author Bo
 *
 */
public class FileService {
	/**
	 * context对象
	 */
	private Context context;
	/**
	 * 构造函数,是的使用此类必须传一个context进来
	 * @param context
	 */
	public FileService(Context context){
		this.context = context;
	}
	/**
	 * 保存内容
	 * @param fileName 文件名称
	 * @param fileContent 文件内容
	 * @throws Exception
	 */
	public void save(String fileName, String fileContent) throws Exception{
	//  MODE_PRIVATE 表示私有的,只能被当前应该使用
		FileOutputStream outStream = context.openFileOutput(fileName, context.MODE_PRIVATE);
		outStream.write(fileContent.getBytes());
	}
}

/**
 * <p>
 * 文件的创建与保存</br/>
 * 2012-02-09
 * </p>
 * @author bo
 *
 */
public class MainFileActivity extends Activity {
	private FileService service ;
	private static final String TAG = "MainFileActivity";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //实例化
        service = new FileService(this);
        
        Button button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				EditText fileNameText = (EditText) findViewById(R.id.fileName);
				EditText fileContentText = (EditText) findViewById(R.id.fileContent);
				//文件名
				String fileName = fileNameText.getText().toString();
				//内容
				String fileContent = fileContentText.getText().toString();
				try {
					service.save(fileName, fileContent);
					Toast.makeText(MainFileActivity.this, R.string.success, 1).show();
				} catch (Exception e) {
					Log.e(TAG, e.toString());
					Toast.makeText(MainFileActivity.this, R.string.error, 1).show();
				}
			}
		});
    }
}

 

文章来源于网络或者作者投稿,若有侵权请联系删除,作者:老钟,如若转载,请注明出处:https://www.laoz.net/221.html

(0)
老钟
上一篇 2012 年 02 月 08 日 下午 4:40
下一篇 2012 年 02 月 16 日 下午 1:06

相关推荐

  • Java 中 finally 语句块的深度辨析【转】

    可不能小看这个简单的 finally,看似简单的问题背后,却隐藏了无数的玄机。接下来我就带您一步一步的揭开这个 finally 的神秘面纱。 问题分析 首先来问大家一个问题:finally 语句块一定会执行吗? 很多人都认为 fi…

    闲话杂谈 2011 年 08 月 16 日
  • Android Please execute ‘adb uninstall com.ims.poc.ui’ in a shell.

    [2010-03-23 11:57:49 - Poc]Re-installation failed due to different application signatures. [2010-03-23 11:57:49 - Poc]You must perform a full uninstall of the application. WARNING: This will remove t…

    闲话杂谈 2012 年 01 月 30 日
  • 多想一点,多做一点[转]

    1 先来看个例子: ‍‍。。。 2 再举个例子: 不知道男同学们有没有帮女同学在自动售货机买过饮料,买完以后顺手递给她,这一切都很自然。 那么你会不会在递给她之前,顺手帮她把瓶子盖拧开? 顺手把某个人的Hi发给对…

    闲话杂谈 2011 年 08 月 26 日
  • 可恨,我被DNS挟持了?打开京东默认跳转到推广链接

    打开京东跳转到推广链接~ 我现在用的是电信网络,经过多方面测试,我怀疑是电信DNS挟持,其实很早之前就发现在浏览器上面输入网址: JD.COM,就会默认跳转到推广的链接,只不过一直没有跟它较劲,今天突发神经,坚…

    2016 年 07 月 01 日
  • 哪个银行信用卡最好用?哪个银行信用卡最好

    四大国有商业银行: 建设银行: 优点: 1、免首年年费,刷三次免次年年费; 2、短信服务免费; 3、取现手续费仅0.5%; 4、挂失手续费50元; 5、有直接针对有车族信用卡,只要有1.4排量以上的车均能轻松办卡。 缺点…

    闲话杂谈 2011 年 10 月 09 日
  • HashMap与ConcurrentHashMap性能测试

    http://blog.csdn.net/liuzhengkang/article/details/2916829 http://blog.sina.com.cn/s/blog_605f5b4f0100qsio.html http://hi.baidu.com/fallen9/blog/item/690e963d56471209bba1670a.html http://www.xue5.com/…

    闲话杂谈 2011 年 08 月 30 日
  • 日语,50音

    偶尔一个机会,需要整理一下日语50音(中文应该是音标)   あ(a) い(i) う(u) え(e) お(o) か(ka) き(ki) く(ku) け(ke) こ(ko) さ(sa) し(shi) す(su) せ(se) そ(so) た(ta) ち(chi) つ(tsu) て(te) と(to) …

    闲话杂谈 2012 年 03 月 01 日
  • eclipse报错,Background Indexer Crash Recovery

    启动eclipse,报错Background Indexer Crash Recovery网上找了很多东西,什么删除jar包等等,解决不了问题干脆一下子把eclipse给删除掉了重新弄一个,搞定

    闲话杂谈 2012 年 06 月 04 日
  • HashMap与ConcurrentHashMap性能对比

    目的是测试HashMap与ConcurrentHashMap性能 package com.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Concurren…

    闲话杂谈 2011 年 08 月 30 日
  • Eclipse快捷键大全(转载)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ 当前行和下面一行交互位置(特别实用,可以省去先剪切,…

    闲话杂谈 2011 年 12 月 19 日