Android对文件的保存操作

来源:互联网 发布:数据精度 英文 编辑:程序博客网 时间:2024/06/07 01:51

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 blocke.printStackTrace();}finally{try {oStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


 

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

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

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

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

原创粉丝点击