第二十篇-如何寫配置檔案
阿新 • • 發佈:2018-10-31
配置檔案不同的格式所用不同函式,可能可以優化
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; importView Codejava.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) throwsIOException { 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 + "\r\n"; } } if (title && !canAdd) { fileContent += allLine + "\r\n"; } 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 + "\r\n"; tag=true; canAdd1=false; } else{ fileContent+=allLine+"\r\n"; } } else{ if(allLine.equals("")){ if(canAdd1){ newLine = variable + "=" + value; fileContent+=newLine+"\r\n"; } fileContent+=allLine+"\r\n"; tag=true; canAdd1=false; } } } } if(fileContent.equals("")){ fileContent="["+section+"]"+"\r\n"; newLine = variable + "=" + value; fileContent+=newLine+"\r\n"; } else{ if(!canAdd&&!tag){ fileContent+="\r\n"+"["+section+"]"+"\r\n"; newLine = variable + "=" + value; fileContent+=newLine+"\r\n"; } if(canAdd1&&!tag){ newLine = variable + "=" + value; fileContent+=newLine+"\r\n"; } } } 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 + "\r\n"; } } if (title && !canAdd) { fileContent += allLine + "\r\n"; } 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 + "\r\n"; tag=true; canAdd1=false; } else{ fileContent+=allLine+"\r\n"; } } else{ if(allLine.equals("")){ if(canAdd1){ newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+"\r\n"; } fileContent+=allLine+"\r\n"; tag=true; canAdd1=false; } } } } if(fileContent.equals("")){ fileContent="["+section+"]"+"\r\n"; newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+"\r\n"; } else{ if(!canAdd&&!tag){ fileContent+="\r\n"+"["+section+"]"+"\r\n"; newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+"\r\n"; } if(canAdd1&&!tag){ newLine= Arrays.stream(arr).collect(Collectors.joining(",")); fileContent+=newLine+"\r\n"; } } } 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(); } } } }View Code
可以寫入log檔案,不過只限於等於和逗號兩類。