第二十篇-如何写配置文件
Posted smart-zihan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二十篇-如何写配置文件相关的知识,希望对你有一定的参考价值。
配置文件不同的格式所用不同函数,可能可以优化
WriteData.java
package com.example.aimee.logtest; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.Log; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public final class WriteData { private final String TAG = "WriteData"; public void writeCfgValue(String file, String section, String variable, String value) throws IOException { String fileContent, allLine, newLine; String getValue = null; File file1 = new File(file); if (!file1.exists()) { file1.createNewFile(); } String path_1 = file1.getParent(); String fileNew = path_1 + "/" + "tmp.ini"; File file_new = new File(fileNew); if (!file_new.exists()) { file_new.createNewFile(); } BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); Boolean canAdd = false; Boolean canAdd1 = false; Boolean title = false; fileContent = ""; Boolean tag=false; try { while ((allLine = bufferedReader.readLine()) != null) { Pattern p; Matcher m; p = Pattern.compile("\[\w+]"); m = p.matcher(allLine); if (m.matches()) { title = true; canAdd = false; canAdd1=false; p = Pattern.compile("\[" + section + "]"); m = p.matcher(allLine); if (m.matches()) { canAdd = true; canAdd1=true; title = false; fileContent += allLine + " "; } } if (title && !canAdd) { fileContent += allLine + " "; } if (!title && canAdd) { Pattern p1; Matcher m1; p1 = Pattern.compile(".*?=.*?"); m1 = p1.matcher(allLine); if (m1.matches()) { String[] strArray = allLine.split("="); getValue = strArray[0].trim(); if (getValue.equalsIgnoreCase(variable)) { newLine = getValue + "=" + value; fileContent += newLine + " "; tag=true; canAdd1=false; } else{ fileContent+=allLine+" "; } } else{ if(allLine.equals("")){ if(canAdd1){ newLine = variable + "=" + value; fileContent+=newLine+" "; } fileContent+=allLine+" "; tag=true; canAdd1=false; } } } } if(fileContent.equals("")){ fileContent="["+section+"]"+" "; newLine = variable + "=" + value; fileContent+=newLine+" "; } else{ if(!canAdd&&!tag){ fileContent+=" "+"["+section+"]"+" "; newLine = variable + "=" + value; fileContent+=newLine+" "; } if(canAdd1&&!tag){ newLine = variable + "=" + value; fileContent+=newLine+" "; } } } catch(IOException ex) { throw ex; }finally { bufferedReader.close(); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileNew, false)); bufferedWriter.write(fileContent); bufferedWriter.flush(); bufferedWriter.close(); } file_new.renameTo(file1); } @RequiresApi(api = Build.VERSION_CODES.N) public void writeCfgValue(String file, String section, String testitem, String sublocation, String min, String max, String expvalue1, String expcalue2, String actvalue1, String actvalue2, String status, String failinfo, String errorcode) throws IOException { String fileContent, allLine, newLine; String getValue = null; String[] arr={testitem,sublocation,min,max,expvalue1,expcalue2,actvalue1,actvalue2,status,failinfo,errorcode}; File file1 = new File(file); if (!file1.exists()) { file1.createNewFile(); } String path_1 = file1.getParent(); String fileNew = path_1 + "/" + "tmp.ini"; File file_new = new File(fileNew); if (!file_new.exists()) { file_new.createNewFile(); } BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); Boolean canAdd = false; Boolean canAdd1 = false; Boolean title = false; fileContent = ""; Boolean tag=false; try { while ((allLine = bufferedReader.readLine()) != null) { Pattern p; Matcher m; p = Pattern.compile("\[\w+]"); m = p.matcher(allLine); if (m.matches()) { title = true; canAdd = false; canAdd1=false; p = Pattern.compile("\[" + section + "]"); m = p.matcher(allLine); if (m.matches()) { canAdd = true; canAdd1=true; title = false; fileContent += allLine + " "; } } if (title && !canAdd) { fileContent += allLine + " "; } if (!title && canAdd) { if (allLine.contains(",")) { String[] strArray = allLine.split(","); getValue = strArray[0].trim(); if (getValue.equalsIgnoreCase(testitem)) { newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent += newLine + " "; tag=true; canAdd1=false; } else{ fileContent+=allLine+" "; } } else{ if(allLine.equals("")){ if(canAdd1){ newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+" "; } fileContent+=allLine+" "; tag=true; canAdd1=false; } } } } if(fileContent.equals("")){ fileContent="["+section+"]"+" "; newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+" "; } else{ if(!canAdd&&!tag){ fileContent+=" "+"["+section+"]"+" "; newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+" "; } if(canAdd1&&!tag){ newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+" "; } } } catch(IOException ex) { throw ex; }finally { bufferedReader.close(); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileNew, false)); Log.i(TAG, "fileContent: "+fileContent); bufferedWriter.write(fileContent); bufferedWriter.flush(); bufferedWriter.close(); } file_new.renameTo(file1); } }
MainActivity.java
package com.example.aimee.logtest; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.os.Environment; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { String path_directory; String file_name; private final int REQUESTCODE=101; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); path_directory = Environment.getExternalStorageDirectory().getAbsolutePath(); path_directory=path_directory+"/"+"save"; file_name = "test4.log"; String path=path_directory+"/"+file_name; if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){ int checkSelfPermission=checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if(checkSelfPermission==PackageManager.PERMISSION_DENIED){ requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE); } } fileMethodSet fileMethodSet=new fileMethodSet(); File file=new File(path); if(!file.exists()){ try { fileMethodSet.create_file(path_directory,file_name); } catch (IOException e) { e.printStackTrace(); } } WriteData writeData=new WriteData(); try { // writeData.writeCfgValue(path,"HEAD1","NAME2","start5"); writeData.writeCfgValue(path,"TESTITEM","cpu111","j08","1004","3004","","","1004","","PASS","",""); } catch (IOException e) { e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==REQUESTCODE){ if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){ Toast.makeText(this,"ok",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"无权限",Toast.LENGTH_LONG).show(); } } } }
可以写入log文件,不过只限于等于和逗号两类。
以上是关于第二十篇-如何写配置文件的主要内容,如果未能解决你的问题,请参考以下文章