android > SDcard读写文件
16lz
2021-12-04
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"/>
更多相关文章
- Pycharm安装PyQt5的详细教程
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android(安卓)删除SD卡文件和文件及创建文件夹和文件
- android driver for ubuntu 手机驱动
- android xml文件操作
- Android(安卓)解决 RecyclerView 嵌套 ScrollView 数据显示不全
- Android(安卓)Studio - Session 'app': Error launching activit
- android 操作文件