android > SDcard读写文件

package sp.mft;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class SpActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          
       
        String filePath = "ip_sensor";	//sdcartpath/ipsensor/
        String fileName = "sensor_list.xml";
        
        //写文件
        writeToXml(SpActivity.this,"i want write in",getSdcardPath()+filePath,fileName);
        
        //读文件 
        String readXml = ReadTxtFile(getSdcardPath()+filePath+"/"+fileName);
        System.out.println(readXml);
        
    }
    写文件  
    public static int writeToXml(Context context, String str,String file_path,String file_name){    
    	int result = 0;
    	File path = new File(file_path);
        File file = new File(file_path+"/"+file_name);
        if (!path.exists()) {
            // 路径不存在? Just 创建
            path.mkdirs();
        }

        if (!file.exists()) {
            // 文件不存在、 Just创建
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(
                    file));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            osw.write(str);
            osw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return result;
    }      
    
    /// 读文件
    public static String ReadTxtFile(String strFilePath)
    {
        String path = strFilePath;
        String content = ""; //文件内容字符串
            //打开文件
            File file = new File(path);
            //如果path是传递过来的参数,可以做一个非目录的判断
            if (file.isDirectory())
            {
                Log.d("TestFile", "The File doesn't not exist.");
            }
            else
            {
                try {
                    InputStream instream = new FileInputStream(file); 
                    if (instream != null) 
                    {
                        InputStreamReader inputreader = new InputStreamReader(instream);
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line;
                        //分行读取
                        while (( line = buffreader.readLine()) != null) {
                            content += line + "\n";
                        }                
                        instream.close();
                    }
                }
                catch (java.io.FileNotFoundException e) 
                {
                    Log.d("TestFile", "The File doesn't not exist.");
                } 
                catch (IOException e) 
                {
                     Log.d("TestFile", e.getMessage());
                }
            }
            return content;
    }
    
    // 获取sdCard路径
    public static String getSdcardPath()
    {
        // 定义一个空字符串,存储sdcard路径
        String sdcardPath = "";
        // 定义一个File类型的变量,存储sdcard的路径
        File sdcardPathFile = null;
        // 判断sdcard是否存在
        boolean isSdcardExist = Environment.getExternalStorageState().equals(
                                                   Environment.MEDIA_MOUNTED);
        if (isSdcardExist)
        {
            // 将路径值赋给变量sdcardPath
            sdcardPathFile = Environment.getExternalStorageDirectory();
            sdcardPath = sdcardPathFile.toString() + "/";
        }
        else
        {
            // 将路径值赋给变量sdcardPath
            sdcardPathFile = Environment.getRootDirectory();
            sdcardPath = sdcardPathFile.toString() + "/";
        }
        // 最后返回一个保存sdcard路径的字符串
        return sdcardPath;
    }    
    //\\
    
}

 

 

然后是是 xml 加入 权限

 

	<!-- 在SDCard中创建与删除文件权限 -->
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
	<!-- 往SDCard写入数据权限 -->
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值