android > SDcard读写文件 android > SDcard读写文件

Java代码 复制代码  收藏代码
  1. package sp.mft;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.FileNotFoundException;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.io.InputStreamReader;   
  11. import java.io.OutputStream;   
  12. import java.io.OutputStreamWriter;   
  13. import java.io.UnsupportedEncodingException;   
  14.   
  15. import android.app.Activity;   
  16. import android.content.Context;   
  17. import android.content.SharedPreferences;   
  18. import android.os.Bundle;   
  19. import android.os.Environment;   
  20. import android.util.Log;   
  21.   
  22. public class SpActivity extends Activity {   
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {   
  26.         super.onCreate(savedInstanceState);   
  27.         setContentView(R.layout.main);             
  28.           
  29.         String filePath = "ip_sensor";  //sdcartpath/ipsensor/   
  30.         String fileName = "sensor_list.xml";   
  31.            
  32.         //写文件   
  33.         writeToXml(SpActivity.this,"i want write in",getSdcardPath()+filePath,fileName);   
  34.            
  35.         //读文件    
  36.         String readXml = ReadTxtFile(getSdcardPath()+filePath+"/"+fileName);   
  37.         System.out.println(readXml);   
  38.            
  39.     }   
  40.     ////写文件     
  41.     public static int writeToXml(Context context, String str,String file_path,String file_name){       
  42.         int result = 0;   
  43.         File path = new File(file_path);   
  44.         File file = new File(file_path+"/"+file_name);   
  45.         if (!path.exists()) {   
  46.             // 路径不存在? Just 创建   
  47.             path.mkdirs();   
  48.         }   
  49.   
  50.         if (!file.exists()) {   
  51.             // 文件不存在、 Just创建   
  52.             try {   
  53.                 file.createNewFile();   
  54.             } catch (IOException e) {   
  55.                 // TODO Auto-generated catch block   
  56.                 e.printStackTrace();   
  57.             }   
  58.         }   
  59.         OutputStreamWriter osw = null;   
  60.         try {   
  61.             osw = new OutputStreamWriter(new FileOutputStream(   
  62.                     file));   
  63.         } catch (FileNotFoundException e1) {   
  64.             // TODO Auto-generated catch block   
  65.             e1.printStackTrace();   
  66.         }   
  67.         try {   
  68.             osw.write(str);   
  69.             osw.close();   
  70.         } catch (IOException e) {   
  71.             // TODO Auto-generated catch block   
  72.             e.printStackTrace();   
  73.         }   
  74.            
  75.         return result;   
  76.     }         
  77.        
  78.     /// 读文件   
  79.     public static String ReadTxtFile(String strFilePath)   
  80.     {   
  81.         String path = strFilePath;   
  82.         String content = ""//文件内容字符串   
  83.             //打开文件   
  84.             File file = new File(path);   
  85.             //如果path是传递过来的参数,可以做一个非目录的判断   
  86.             if (file.isDirectory())   
  87.             {   
  88.                 Log.d("TestFile""The File doesn't not exist.");   
  89.             }   
  90.             else  
  91.             {   
  92.                 try {   
  93.                     InputStream instream = new FileInputStream(file);    
  94.                     if (instream != null)    
  95.                     {   
  96.                         InputStreamReader inputreader = new InputStreamReader(instream);   
  97.                         BufferedReader buffreader = new BufferedReader(inputreader);   
  98.                         String line;   
  99.                         //分行读取   
  100.                         while (( line = buffreader.readLine()) != null) {   
  101.                             content += line + "\n";   
  102.                         }                   
  103.                         instream.close();   
  104.                     }   
  105.                 }   
  106.                 catch (java.io.FileNotFoundException e)    
  107.                 {   
  108.                     Log.d("TestFile""The File doesn't not exist.");   
  109.                 }    
  110.                 catch (IOException e)    
  111.                 {   
  112.                      Log.d("TestFile", e.getMessage());   
  113.                 }   
  114.             }   
  115.             return content;   
  116.     }   
  117.        
  118.     // 获取sdCard路径   
  119.     public static String getSdcardPath()   
  120.     {   
  121.         // 定义一个空字符串,存储sdcard路径   
  122.         String sdcardPath = "";   
  123.         // 定义一个File类型的变量,存储sdcard的路径   
  124.         File sdcardPathFile = null;   
  125.         // 判断sdcard是否存在   
  126.         boolean isSdcardExist = Environment.getExternalStorageState().equals(   
  127.                                                    Environment.MEDIA_MOUNTED);   
  128.         if (isSdcardExist)   
  129.         {   
  130.             // 将路径值赋给变量sdcardPath   
  131.             sdcardPathFile = Environment.getExternalStorageDirectory();   
  132.             sdcardPath = sdcardPathFile.toString() + "/";   
  133.         }   
  134.         else  
  135.         {   
  136.             // 将路径值赋给变量sdcardPath   
  137.             sdcardPathFile = Environment.getRootDirectory();   
  138.             sdcardPath = sdcardPathFile.toString() + "/";   
  139.         }   
  140.         // 最后返回一个保存sdcard路径的字符串   
  141.         return sdcardPath;   
  142.     }       
  143.     //\\   
  144.        
  145. }  
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 加入 权限

Xml代码 复制代码  收藏代码
  1. <!-- 在SDCard中创建与删除文件权限 -->  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  3. <!-- 往SDCard写入数据权限 -->  
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
Java代码 复制代码  收藏代码
  1. package sp.mft;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.FileNotFoundException;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.io.InputStreamReader;   
  11. import java.io.OutputStream;   
  12. import java.io.OutputStreamWriter;   
  13. import java.io.UnsupportedEncodingException;   
  14.   
  15. import android.app.Activity;   
  16. import android.content.Context;   
  17. import android.content.SharedPreferences;   
  18. import android.os.Bundle;   
  19. import android.os.Environment;   
  20. import android.util.Log;   
  21.   
  22. public class SpActivity extends Activity {   
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {   
  26.         super.onCreate(savedInstanceState);   
  27.         setContentView(R.layout.main);             
  28.           
  29.         String filePath = "ip_sensor";  //sdcartpath/ipsensor/   
  30.         String fileName = "sensor_list.xml";   
  31.            
  32.         //写文件   
  33.         writeToXml(SpActivity.this,"i want write in",getSdcardPath()+filePath,fileName);   
  34.            
  35.         //读文件    
  36.         String readXml = ReadTxtFile(getSdcardPath()+filePath+"/"+fileName);   
  37.         System.out.println(readXml);   
  38.            
  39.     }   
  40.     ////写文件     
  41.     public static int writeToXml(Context context, String str,String file_path,String file_name){       
  42.         int result = 0;   
  43.         File path = new File(file_path);   
  44.         File file = new File(file_path+"/"+file_name);   
  45.         if (!path.exists()) {   
  46.             // 路径不存在? Just 创建   
  47.             path.mkdirs();   
  48.         }   
  49.   
  50.         if (!file.exists()) {   
  51.             // 文件不存在、 Just创建   
  52.             try {   
  53.                 file.createNewFile();   
  54.             } catch (IOException e) {   
  55.                 // TODO Auto-generated catch block   
  56.                 e.printStackTrace();   
  57.             }   
  58.         }   
  59.         OutputStreamWriter osw = null;   
  60.         try {   
  61.             osw = new OutputStreamWriter(new FileOutputStream(   
  62.                     file));   
  63.         } catch (FileNotFoundException e1) {   
  64.             // TODO Auto-generated catch block   
  65.             e1.printStackTrace();   
  66.         }   
  67.         try {   
  68.             osw.write(str);   
  69.             osw.close();   
  70.         } catch (IOException e) {   
  71.             // TODO Auto-generated catch block   
  72.             e.printStackTrace();   
  73.         }   
  74.            
  75.         return result;   
  76.     }         
  77.        
  78.     /// 读文件   
  79.     public static String ReadTxtFile(String strFilePath)   
  80.     {   
  81.         String path = strFilePath;   
  82.         String content = ""//文件内容字符串   
  83.             //打开文件   
  84.             File file = new File(path);   
  85.             //如果path是传递过来的参数,可以做一个非目录的判断   
  86.             if (file.isDirectory())   
  87.             {   
  88.                 Log.d("TestFile""The File doesn't not exist.");   
  89.             }   
  90.             else  
  91.             {   
  92.                 try {   
  93.                     InputStream instream = new FileInputStream(file);    
  94.                     if (instream != null)    
  95.                     {   
  96.                         InputStreamReader inputreader = new InputStreamReader(instream);   
  97.                         BufferedReader buffreader = new BufferedReader(inputreader);   
  98.                         String line;   
  99.                         //分行读取   
  100.                         while (( line = buffreader.readLine()) != null) {   
  101.                             content += line + "\n";   
  102.                         }                   
  103.                         instream.close();   
  104.                     }   
  105.                 }   
  106.                 catch (java.io.FileNotFoundException e)    
  107.                 {   
  108.                     Log.d("TestFile""The File doesn't not exist.");   
  109.                 }    
  110.                 catch (IOException e)    
  111.                 {   
  112.                      Log.d("TestFile", e.getMessage());   
  113.                 }   
  114.             }   
  115.             return content;   
  116.     }   
  117.        
  118.     // 获取sdCard路径   
  119.     public static String getSdcardPath()   
  120.     {   
  121.         // 定义一个空字符串,存储sdcard路径   
  122.         String sdcardPath = "";   
  123.         // 定义一个File类型的变量,存储sdcard的路径   
  124.         File sdcardPathFile = null;   
  125.         // 判断sdcard是否存在   
  126.         boolean isSdcardExist = Environment.getExternalStorageState().equals(   
  127.                                                    Environment.MEDIA_MOUNTED);   
  128.         if (isSdcardExist)   
  129.         {   
  130.             // 将路径值赋给变量sdcardPath   
  131.             sdcardPathFile = Environment.getExternalStorageDirectory();   
  132.             sdcardPath = sdcardPathFile.toString() + "/";   
  133.         }   
  134.         else  
  135.         {   
  136.             // 将路径值赋给变量sdcardPath   
  137.             sdcardPathFile = Environment.getRootDirectory();   
  138.             sdcardPath = sdcardPathFile.toString() + "/";   
  139.         }   
  140.         // 最后返回一个保存sdcard路径的字符串   
  141.         return sdcardPath;   
  142.     }       
  143.     //\\   
  144.        
  145. }  
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 加入 权限

Xml代码 复制代码  收藏代码
  1. <!-- 在SDCard中创建与删除文件权限 -->  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  3. <!-- 往SDCard写入数据权限 -->  
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  

猜你喜欢

转载自zhouxuebao87.iteye.com/blog/1609706
今日推荐