Android对文件的保存操作

1.文件可以保存在手机内存中,也可以保存在SD卡上面,保存在SD卡上要申请读写SDS卡权限,权限如下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

①保存在手机上

	public void saveFile(String fileName,String contentStr) throws IOException{
		FileOutputStream fileOutputStream=context.openFileOutput(fileName, Context.MODE_PRIVATE);
		fileOutputStream.write(contentStr.getBytes());
	}


②保存在SD卡上

	public void savefile2SD(String fileNameStr, String contentStr) throws FileNotFoundException {
		File file=new File(Environment.getExternalStorageDirectory(), fileNameStr);
		OutputStream oStream=new FileOutputStream(file);
		byte[] data=contentStr.getBytes();
		try {
			oStream.write(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				oStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}


关于界面,以前见到淘宝上面输入框里面有提示文字,然后鼠标点进去后文字消失,等待用户输入的效果

    <EditText
        android:id="@+id/fileNameID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="请输入文件名称">

android:hint="请输入文件名称"------就是这样一行代码

还有就是要注意一下:在模拟器上面如果输入中文的话,那么保存后就有乱码,保存文件后导出到PC的话会发现找不到该文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值