Android保存配置文件内容到本地(txt、xml两种)
生活随笔
收集整理的這篇文章主要介紹了
Android保存配置文件内容到本地(txt、xml两种)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在做項目的時候難免需要保存一下配置文件,我們經常使用的就是SharedPreferences,但是當我們清除掉緩存或者卸載后重新安裝這些配置文件內容就不存在了,當我們想卸載后重新安裝這些配置文件還在,那只能將這些配置文件保存到本地了,用的時候去讀取,保存本地有兩種,保存為TXT或者是保存為xml
第一種:把配置文件保存為TXT到本地
1、我們需要一個操作文件的工具類,這里已經寫好
import android.graphics.Bitmap; import android.os.Environment;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile;/*** date : 2021/4/23 2:05 PM* description :文件工具類*/ public class FileUtils {/*** 讀取txt文件的內容** @param filePath 想要讀取的文件對象* @return 返回文件內容*/public static String txt2String(String filePath) {File file = new File(filePath);if (!file.exists()) {return "";}StringBuilder result = new StringBuilder();try {// 構造一個BufferedReader類來讀取文件BufferedReader br = new BufferedReader(new FileReader(file));String s = null;// 使用readLine方法,一次讀一行while ((s = br.readLine()) != null) {result.append(System.lineSeparator() + s);}br.close();} catch (Exception e) {e.printStackTrace();}return result.toString();}/*** 寫入TXT文件*/public static boolean writeTxtFile(String content, String filePath) {File file = new File(filePath);if (!file.exists()) {return false;}RandomAccessFile mm = null;boolean flag = false;FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(file);fileOutputStream.write(content.getBytes("utf-8"));fileOutputStream.close();flag = true;} catch (Exception e) {e.printStackTrace();}return flag;}/*** Checks if is sd card available.檢查SD卡是否可用*/public static boolean isSdCardAvailable() {return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}/*** Gets the SD root file.獲取SD卡根目錄*/public static File getSDRootFile() {if (isSdCardAvailable()) {return Environment.getExternalStorageDirectory();} else {return null;}}/*** 獲取導入圖片文件的目錄信息*/public static File getBatchImportDirectory() {// 獲取根目錄File sdRootFile = getSDRootFile();File file = null;if (sdRootFile != null && sdRootFile.exists()) {file = new File(sdRootFile, "Face-Import");if (!file.exists()) {file.mkdirs();}}return file;}/*** 獲取導入圖片成功的目錄信息*/public static File getBatchImportSuccessDirectory() {File sdRootFile = getSDRootFile();File file = null;if (sdRootFile != null && sdRootFile.exists()) {file = new File(sdRootFile, "Success-Import");if (!file.exists()) {file.mkdirs();}}return file;}/*** 判斷文件是否存在*/public static File isFileExist(String fileDirectory, String fileName) {File file = new File(fileDirectory + "/" + fileName);try {if (!file.exists()) {return null;}} catch (Exception e) {return null;}return file;}/*** 刪除文件*/public static void deleteFile(String filePath) {try {// 找到文件所在的路徑并刪除該文件File file = new File(filePath);file.delete();} catch (Exception e) {e.printStackTrace();}}/** 獲取不帶擴展名的文件名* */public static String getFileNameNoEx(String filename) {if ((filename != null) && (filename.length() > 0)) {int dot = filename.lastIndexOf('.');if ((dot > -1) && (dot < (filename.length()))) {return filename.substring(0, dot);}}return filename;}/*** 保存圖片*/public static boolean saveBitmap(File file, Bitmap bitmap) {FileOutputStream out = null;try {out = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);return true;} catch (Exception e) {e.printStackTrace();} finally {try {if (out != null) {out.close();}} catch (Exception e) {e.printStackTrace();}}return false;}public static boolean copyFile(String oldPath, String newPath) {InputStream inStream = null;FileOutputStream fs = null;boolean result = false;try {int bytesum = 0;int byteread = 0;File oldfile = new File(oldPath);// 判斷目錄是否存在File newfile = new File(newPath);File newFileDir = new File(newfile.getPath().replace(newfile.getName(), ""));if (!newFileDir.exists()) {newFileDir.mkdirs();}if (oldfile.exists()) { // 文件存在時inStream = new FileInputStream(oldPath); // 讀入原文件fs = new FileOutputStream(newPath);byte[] buffer = new byte[1444];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread; // 字節數 文件大小System.out.println(bytesum);fs.write(buffer, 0, byteread);}result = true;} else {result = false;}} catch (Exception e) {System.out.println("復制單個文件操作出錯");e.printStackTrace();} finally {if (inStream != null) {try {inStream.close();} catch (IOException e) {e.printStackTrace();}}if (fs != null) {try {fs.close();} catch (IOException e) {e.printStackTrace();}}}return result;}}2,、需要一個保存讀取文件的工具類,同時檢測文件是否存在,文件里面是否有內容等
import android.os.Environment; import android.util.Log; import org.json.JSONObject; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Field; import java.util.regex.Matcher; import java.util.regex.Pattern;/*** Created by Forrest.* User: Administrator* Date: 2021/5/7* Description:*/ public class SaveConfigUtils {//設置gateFaceConfig的根路徑public static final String folder = Environment.getExternalStorageDirectory() + File.separator + "Settings";// 配置文件路徑public static final String filePath = folder + "/" + "Ceshi.txt";//判斷文件是否存在public static boolean isConfigExit() {File file1 = new File(folder);//判斷Settings文件夾是否存在if (!file1.exists()) {file1.mkdirs();}//判斷gateFaceConfig.txt文件是否存在,存在返回true,不存在創建文件并更新文件File file = new File(filePath);if (file.exists()) {return true;} else {try {file.createNewFile();modityJson();} catch (IOException e) {e.printStackTrace();return false;}return true;}}public static String configMessage() {String configMessage = FileUtils.txt2String(filePath);return configMessage;}/*** 判斷SDCard是否可用** @return*/public static boolean isSDCardEnable() {return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}/*** 讀取配置文件內容** @return*/public static Boolean initConfig() {//得到gateFaceConfig.txt文件里面的內容String configMessage = FileUtils.txt2String(filePath);//gateFaceConfig.txt里面的內容為空返回falseif (configMessage.equals("")) {Log.e("facesdk", "文件不存在");return false;}//gateFaceConfig.txt里面的內容不為空就為SingleBaseConfig賦值Log.e("人臉識別", filePath + "文件存在" + configMessage);JSONObject jsonObject = null;try {jsonObject = new JSONObject(configMessage);if (!identify(jsonObject)) {return false;}SingleBaseConfig.getBaseConfig().setName(jsonObject.getString("name"));// RGB檢測幀回顯SingleBaseConfig.getBaseConfig().setSex(jsonObject.getString("sex"));// NIR或depth實時視頻預覽SingleBaseConfig.getBaseConfig().setAge(jsonObject.getString("age"));// 默認為false。可選項為"true"、"false",是否開啟調試顯示,將會作用到所有視頻流識別頁面,包含1:N、1:1采集人臉圖片環節。return true;} catch (Exception e) {e.printStackTrace();Log.e("facesdk", "文件內容異常,請檢測是否規范");return false;}}// 校驗sdcard里的txt文件內容是否正常public static boolean identify(JSONObject jsonObject) {try {Boolean display = (Boolean) jsonObject.get("display");Boolean isNirOrDepth = (Boolean) jsonObject.get("isNirOrDepth");Boolean debug = (Boolean) jsonObject.get("debug");int videoDirection = Integer.parseInt(jsonObject.getString("videoDirection"));if (!(videoDirection == 0 || videoDirection == 90 || videoDirection == 180 || videoDirection == 270)) {return false;}String detectFrame = (String) jsonObject.get("detectFrame");if (!(detectFrame.equals("wireframe") || detectFrame.equals("fixedarea"))) {return false;}// int radius = (int) jsonObject.get("radius");int detectDirection = (int) jsonObject.get("detectDirection");if (!(detectDirection == 0 || detectDirection == 90 || detectDirection == 180|| detectDirection == 270)) {return false;}String trackType = (String) jsonObject.get("trackType");if (!(trackType.equals("max") || trackType.equals("first") || trackType.equals("none"))) {return false;}int minimumFace = (int) jsonObject.get("minimumFace");if (minimumFace < 30) {return false;}float blur = Float.valueOf(jsonObject.get("blur") + "");if (blur > 1 || blur < 0) {return false;}int illumination = (int) jsonObject.get("illumination");if (illumination < 0 || illumination > 255) {return false;}float gesture = Float.valueOf(jsonObject.get("gesture") + "");float pitch = Float.valueOf(jsonObject.get("pitch") + "");if (pitch < -90 || pitch > 90) {return false;}float roll = Float.valueOf(jsonObject.get("roll") + "");if (roll < -90 || roll > 90) {return false;}float yaw = Float.valueOf(jsonObject.get("yaw") + "");if (yaw < -90 || yaw > 90) {return false;}float occlusion = Float.valueOf(jsonObject.get("occlusion") + "");if (occlusion < 0 || occlusion > 1) {return false;}float leftEye = Float.valueOf(jsonObject.get("leftEye") + "");if (leftEye < 0 || leftEye > 1) {return false;}float rightEye = Float.valueOf(jsonObject.get("rightEye") + "");if (rightEye < 0 || rightEye > 1) {return false;}float nose = Float.valueOf(jsonObject.get("nose") + "");if (nose < 0 || nose > 1) {return false;}float mouth = Float.valueOf(jsonObject.get("mouth") + "");if (mouth < 0 || mouth > 1) {return false;}float leftCheek = Float.valueOf(jsonObject.get("leftCheek") + "");if (leftCheek < 0 || leftCheek > 1) {return false;}float rightCheek = Float.valueOf(jsonObject.get("rightCheek") + "");if (rightCheek < 0 || rightCheek > 1) {return false;}float chinContour = Float.valueOf(jsonObject.get("chinContour") + "");if (chinContour < 0 || chinContour > 1) {return false;}float completeness = Float.valueOf(jsonObject.get("completeness") + "");if (completeness < 0 || completeness > 1) {return false;}int rgbAndNirThreshold = Integer.valueOf(jsonObject.get("rgbAndNirThreshold") + "");if (rgbAndNirThreshold < 0 || rgbAndNirThreshold > 100) {return false;}int cameraLightThreshold = Integer.valueOf(jsonObject.get("cameraLightThreshold") + "");if (cameraLightThreshold < 0 || cameraLightThreshold > 100) {return false;}int liveThreshold = Integer.valueOf(jsonObject.get("liveThreshold") + "");if (liveThreshold < 0 || liveThreshold > 100) {return false;}int idThreshold = Integer.valueOf(jsonObject.get("IdThreshold") + "");if (idThreshold < 0 || idThreshold > 100) {return false;}int activeModel = Integer.valueOf(jsonObject.get("activeModel") + "");if (!(activeModel == 1 || activeModel == 2 || activeModel == 3)) {return false;}int timeLapse = Integer.valueOf(jsonObject.get("timeLapse") + "");int type = Integer.valueOf(jsonObject.get("type") + "");if (!(type == 0 || type == 1 || type == 2 || type == 3 || type == 4)) {return false;}float rgbLiveScore = Float.valueOf(jsonObject.get("rgbLiveScore") + "");if (rgbLiveScore < 0 || rgbLiveScore > 1) {return false;}float nirLiveScore = Float.valueOf(jsonObject.get("nirLiveScore") + "");if (nirLiveScore < 0 || nirLiveScore > 1) {return false;}float depthLiveScore = Float.valueOf(jsonObject.get("depthLiveScore") + "");if (depthLiveScore < 0 || depthLiveScore > 1) {return false;}int cameraType = jsonObject.getInt("cameraType");if (!(cameraType == 0 || cameraType == 1 || cameraType == 2 || cameraType == 3 ||cameraType == 4 || cameraType == 5 || cameraType == 6)) {return false;}int mirrorRGB = jsonObject.getInt("mirrorRGB");if (!(mirrorRGB == 0 || mirrorRGB == 1)) {return false;}int mirrorNIR = jsonObject.getInt("mirrorNIR");if (!(mirrorNIR == 0 || mirrorNIR == 1)) {return false;}int getBestImageScore = jsonObject.getInt("bestImageScore");if (getBestImageScore < 0 || getBestImageScore > 100) {return false;}int rgbAndNirWidth = jsonObject.getInt("rgbAndNirWidth");int rgbAndNirHeight = jsonObject.getInt("rgbAndNirHeight");int depthWidth = jsonObject.getInt("depthWidth");int depthHeight = jsonObject.getInt("depthHeight");} catch (Exception e) {String errorMessage = getErrorInfoFromException(e);e.printStackTrace();Log.e("facesdk", "文件內容格式異常,請檢測是否規范");return false;}return true;}/*** 修改配置文件內容并重新讀取配置*/public static boolean modityJson() {JSONObject jsonObject = new JSONObject();try {jsonObject.put("name", SingleBaseConfig.getBaseConfig().getName());jsonObject.put("sex", SingleBaseConfig.getBaseConfig().getSex());jsonObject.put("age", SingleBaseConfig.getBaseConfig().getAge());// 修改內容寫入配置文件FileUtils.writeTxtFile(jsonObject.toString(), filePath);// 重新讀取配置文件內容initConfig();return true;} catch (Exception e) {e.printStackTrace();return false;}}public static String getErrorInfoFromException(Exception e) {try {StringWriter sw = new StringWriter();PrintWriter pw = new PrintWriter(sw);e.printStackTrace(pw);return "\r\n" + sw.toString() + "\r\n";} catch (Exception e2) {return "bad getErrorInfoFromException";}}/*** 判斷數字正則表達式** @param str* @return*/public boolean isNumeric(String str) {Pattern pattern = Pattern.compile("[0-9]");Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;}/*** 判斷字符正則表達式** @param str* @return*/public boolean isString(String str) {return str.matches("[a-zA-Z]+");}// 對象屬性賦值public static <T> T gotObjectByObject(Object object, Class<T> clazz) throws Exception {T t = null;if (clazz != null && object != null) {t = clazz.newInstance();Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {field.setAccessible(true);String key = field.getName();try {Field field1 = object.getClass().getDeclaredField(key);field1.setAccessible(true);Object val = field1.get(object);field.set(t, val);} catch (Exception e) {t = null;System.out.println(object.getClass().getName() + "沒有該屬性: " + key);}}}return t;} }3、配置一下需要保存的參數
/*** Created by Forrest.* User: Administrator* Date: 2021/5/7* Description:*/ public class FileConfig {private String name="張三";private String sex="男";private String age="12";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;} }4、配置FileConfig為單例
/*** date : 2021/4/23 11:23 AM* description :配置BaseConfig單例*/ public class SingleBaseConfig {private static FileConfig baseConfig;private SingleBaseConfig() {}public static FileConfig getBaseConfig() {if (baseConfig == null) {baseConfig = new FileConfig();}return baseConfig;}public static void copyInstance(FileConfig result) {baseConfig = result;} }5、然后就是保存和讀取的操作了
@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_savetxt:SingleBaseConfig.getBaseConfig().setName(mEtName.getText().toString());SingleBaseConfig.getBaseConfig().setSex(mEtSex.getText().toString());SingleBaseConfig.getBaseConfig().setAge(mEtAge.getText().toString());SaveConfigUtils.modityJson();break;case R.id.btn_readtxt:String name = SingleBaseConfig.getBaseConfig().getName();String sex = SingleBaseConfig.getBaseConfig().getSex();String age = SingleBaseConfig.getBaseConfig().getAge();mTvShowtxt.setText(SaveConfigUtils.configMessage());Log.e("Ceshi文件", "name=" + name);Log.e("Ceshi文件", "sex=" + sex);Log.e("Ceshi文件", "age=" + age);break;}}第二種、把配置文件保存為xml到本地
1、保存方法
/*** 保存配置文件到本地*/public void setConfig() {try {File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Settings", "Ceshi.xml");Log.e(TAG, "" + file);FileOutputStream fos = new FileOutputStream(file);// 獲得一個序列化工具XmlSerializer serializer = Xml.newSerializer();serializer.setOutput(fos, "utf-8");// 設置文件頭serializer.startDocument("utf-8", true);serializer.startTag(null, "persons");serializer.startTag(null, "person");serializer.attribute(null, "id", String.valueOf(0));// TODO 寫入姓名serializer.startTag(null, "name");serializer.text(mEtName.getText().toString());serializer.endTag(null, "name");// TODO 寫入性別serializer.startTag(null, "sex");serializer.text(mEtSex.getText().toString());serializer.endTag(null, "sex");// TODO 寫入年紀serializer.startTag(null, "age");serializer.text(mEtAge.getText().toString());serializer.endTag(null, "age");serializer.endTag(null, "person");serializer.endTag(null, "persons");serializer.endDocument();fos.close();Toast.makeText(SaveConfigActivity.this, "保存成功", Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(SaveConfigActivity.this, "保存失敗", Toast.LENGTH_SHORT).show();}}2、讀取方法
/*** 讀取本地配置文件*/public void getConfig() {try {File path = new File(Environment.getExternalStorageDirectory() + File.separator + "Settings", "Ceshi.xml");FileInputStream fis = new FileInputStream(path);// 獲得pull解析器對象XmlPullParser parser = Xml.newPullParser();// 指定解析的文件和編碼格式parser.setInput(fis, "utf-8");int eventType = parser.getEventType(); // 獲得事件類型String id = null;String name = null;String sex = null;String age = null;while (eventType != XmlPullParser.END_DOCUMENT) {String tagName = parser.getName(); // 獲得當前節點的名稱switch (eventType) {case XmlPullParser.START_TAG: // 當前等于開始節點 <personif ("persons".equals(tagName)) { // <persons} else if ("person".equals(tagName)) { // <person id="1"id = parser.getAttributeValue(null, "id");} else if ("name".equals(tagName)) { // <versioncodename = parser.nextText();} else if ("sex".equals(tagName)) { // <soakingvoicesex = parser.nextText();} else if ("age".equals(tagName)) { // <voicepromptage = parser.nextText();}break;case XmlPullParser.END_TAG: // </personsif ("person".equals(tagName)) {Log.e(TAG, "id---" + id);Log.e(TAG, "name---" + name);Log.e(TAG, "sex---" + sex);Log.e(TAG, "age---" + age);mTvShowxml.setText("name:" + name + "\rsex:" + sex + "\rage:" + age);}break;default:break;}eventType = parser.next(); // 獲得下一個事件類型}} catch (Exception e) {e.printStackTrace();} finally {}}兩種方法都可以保存配置文件到本地,另外SharedPreferences現在已經被mmvk所代替,下面介紹下mmvk的使用方法
mmvk
1、添加依賴
implementation 'com.tencent:mmkv-static:1.0.23'2、添加Application配置,放到onCreate里面
//緩存的文件在Settings文件夾下的mmvk文件夾 String dir = Environment.getExternalStorageDirectory() + File.separator + "Settings"+"/mmvk"; MMKV.initialize(dir); SpUtils.getInstance();3、使用方法
//保存 SpUtils.encode("int",10); SpUtils.encode("bool",false); SpUtils.encode("long",10); SpUtils.encode("float",10.f); SpUtils.encode("double",10.5); SpUtils.encode("string","10"); byte[] bytes = {'m', 'm', 'k', 'v'}; SpUtils.encode("bytes",bytes); //讀取 LogUtils.e(SpUtils.decodeInt("int")); LogUtils.e(SpUtils.decodeBoolean("bool")); LogUtils.e(SpUtils.decodeLong("long")); LogUtils.e(SpUtils.decodeFloat("float")); LogUtils.e(SpUtils.decodeDouble("double")); LogUtils.e(SpUtils.decodeString("string")); LogUtils.e(SpUtils.decodeBytes("bytes")); package com.dhy.health.saveconfig;import android.os.Parcelable;import com.tencent.mmkv.MMKV;import java.util.Collections; import java.util.Set;/*** Created by Forrest.* User: Administrator* Date: 2021/3/25* Description:*/ public class SpUtils {private static SpUtils mInstance;private static MMKV mv;private SpUtils() {mv = MMKV.defaultMMKV();}/*** 初始化MMKV,只需要初始化一次,建議在Application中初始化**/public static SpUtils getInstance() {if (mInstance == null) {synchronized (SpUtils.class) {if (mInstance == null) {mInstance = new SpUtils();}}}return mInstance;}/*** 保存數據的方法,我們需要拿到保存數據的具體類型,然后根據類型調用不同的保存方法** @param key* @param object*/public static void encode(String key, Object object) {if (object instanceof String) {mv.encode(key, (String) object);} else if (object instanceof Integer) {mv.encode(key, (Integer) object);} else if (object instanceof Boolean) {mv.encode(key, (Boolean) object);} else if (object instanceof Float) {mv.encode(key, (Float) object);} else if (object instanceof Long) {mv.encode(key, (Long) object);} else if (object instanceof Double) {mv.encode(key, (Double) object);} else if (object instanceof byte[] ) {mv.encode(key, (byte[]) object);} else {mv.encode(key, object.toString());}}public static void encodeSet(String key, Set<String> sets) {mv.encode(key, sets);}public static void encodeParcelable(String key, Parcelable obj) {mv.encode(key, obj);}/*** 得到保存數據的方法,我們根據默認值得到保存的數據的具體類型,然后調用相對于的方法獲取值*/public static Integer decodeInt(String key) {return mv.decodeInt(key, 0);}public static Double decodeDouble(String key) {return mv.decodeDouble(key, 0.00);}public static Long decodeLong(String key) {return mv.decodeLong(key, 0L);}public static Boolean decodeBoolean(String key) {return mv.decodeBool(key, false);}public static Float decodeFloat(String key) {return mv.decodeFloat(key, 0F);}public static byte[] decodeBytes(String key) {return mv.decodeBytes(key);}public static String decodeString(String key) {return mv.decodeString(key,"");}public static Set<String> decodeStringSet(String key) {return mv.decodeStringSet(key, Collections.<String>emptySet());}public static Parcelable decodeParcelable(String key) {return mv.decodeParcelable(key, null);}/*** 移除某個key對** @param key*/public static void removeKey(String key) {mv.removeValueForKey(key);}/*** 清除所有key*/public static void clearAll() {mv.clearAll();}}項目地址,需要的加自行下載:SaveConfig.rar_android可修改的配置寫入配置文件還是txt好-互聯網文檔類資源-CSDN下載
總結
以上是生活随笔為你收集整理的Android保存配置文件内容到本地(txt、xml两种)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: β射线与哪些物质可产生较高的韧致辐射_北
- 下一篇: Topological Data Ana