android的sdcard文件的读取和保存详细介绍

1.sdcard文件的保存

sdcardSave

step1. 编写如下代码:

package com.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
/*sdcar文件内容的输出
 * */
public class FileSdCardSaveActivity extends Activity {
 /** Called when the activity is first created. */
 private static final String FILENAME = "mymn.txt";// 设置文件的名称
 private static final String DIR = "mndata";// 操作文件夹得名称

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // 判断sdscard是否存在
  if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {
   //
   File file = new File(Environment.getExternalStorageDirectory()+File.separator+DIR+File.separator+FILENAME);// 定义要操作的文件
   if (!file.getParentFile().exists()) {
    file.getParentFile().mkdirs();// 创建父文件夹路径
   }
   PrintStream outStream = null;
   try {
    outStream = new PrintStream(new FileOutputStream(file));
    outStream.println("3Q大战又来了,嘿嘿");
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    if (outStream != null) {
     outStream.close();
    }
   }
  }else {
   Toast.makeText(this,"保存失败,sd卡不存在",Toast.LENGTH_LONG ).show();
  }

 }
}

 

step2: 配置sdcard写权限:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <!-- 配置sdcard写权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FileSdCardSaveActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

  step3.查看是否写进去。

 

Enviroment定义的常量和方法:

env

2.sdcard文件的读取

sdcardRead

step1.在res,下增加raw文件夹,添加要读取的mymldn.txt文件

step2.在main.xml中编写布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       android:id="@+id/msg" />

</LinearLayout>

 

step3.编写如下代码

package com.activity;

import java.io.InputStream;
import java.util.Scanner;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

/**
 * sdcard文件的读取
 * @author Administrator
 *
 */
public class SdcardFileReadActivity extends Activity {
 
 private TextView msgView = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  this.msgView = (TextView) findViewById(R.id.msg);
  Resources resources = super.getResources();// 资源类操作
  InputStream inputStream = resources.openRawResource(R.raw.mymldn);// 为读取的内容设置输入流
  
Scanner scanner = new Scanner(inputStream);
  StringBuffer buffer = new StringBuffer();
  while (scanner.hasNext()) {
   buffer.append(scanner.next()).append("\n");
  }
  scanner.close();
  try {
   inputStream.close();

  } catch (Exception e) {
   // TODO: handle exception
  }
  this.msgView.setText(buffer);

 }
}

 

读取资源文件

       在android系统中,进行资源的读取,资源文件的id都会自动通过R.java这个类生成,可以通过android.content.res.Resources 类读取。使用Resources类的public InputStream openRawResource(int id)方法;

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值