sd卡文件存储和读取
2015-10-26 16:10
337 查看
package com.example.filedemo1; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { private static final String FILENAME = "/mnt/sdcard/neusoft/jmz.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); File file = new File(FILENAME); //判断文件是否存在,如果不存在创建 if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file)); out.println("中国吉林长春"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out!=null){ out.close(); } } } }
必须配置权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
tv = (TextView) findViewById(R.id.tv); // 判断sd卡是否挂载 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 得到sd卡名称 File file = new File(Environment.getExternalStorageDirectory() + File.separator + DIR + File.separator + FILENAME); Scanner sc = null; try { //判断文件是否存在 if (file.getParentFile().exists()) { sc = new Scanner(new FileInputStream(file)); while (sc.hasNext()) { tv.append(sc.next()); } }else{ Toast.makeText(this, "不存在", 0).show(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (sc != null) { sc.close(); } } } else { Toast.makeText(this, "不存在sd卡", 0).show(); }
相关文章推荐
- E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
- .NET在EF中使用sql,用动态类吧!
- Android开发之旅:HelloWorld项目的目录结构
- linux 下部分手机无法被adb连接的情况
- 欢迎使用CSDN-markdown编辑器
- 第7周项目6停车场模拟
- SQLSERVER SQL性能优化
- 技术小白的C语言之路
- TF-IDF
- C++设计模式之原型模式(改编自C++设计模式)
- A Singular Value Decomposition Approach For Recommendation Systems
- Command Line -- Lesson 1
- 关于php、js弹框及自动关闭、定时跳转
- MapReduce在实际编程“I/O”
- nutch-介绍
- 第九周项目1-猴子选大王(数组版)
- linux系统日志
- 061.Arg Define 宏的更严谨的写法
- Android开发之旅:环境搭建及HelloWorld
- LeetCode:Move Zeroes