import android.util.Log;
import android.text.BidiFormatter;
import android.os.Build;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Uitle{
public static final String TAG = "Uitle";
public static final String PRODUCTINFO_DIR = "/mnt/vendor/productinfo"; //要执行的文件路径
public static final String PRODUCTINFO_DIR_FILENAME = "/mnt/vendor/productinfo/versionNumber";
private static File BOOT_VERSION_FILE = new File(PRODUCTINFO_DIR/*文件路径*/, "versionNumber"/*文件名字*/);
private static String ro_build_display_id ="jjjj";
/*调用start*/
public static void main (String[] args){
//创建文件
try {
write(BOOT_VERSION_FILE,ro_build_display_id);
}catch (Exception e) {
Log.d(TAG, "Write versionNumberBoot fail!");
}
//追加内容
addWrite(PRODUCTINFO_DIR_FILENAME, ro_build_display_id);
//读取文件内容
readFileByLines(PRODUCTINFO_DIR_FILENAME);
//修改文件内容
modifyFile(PRODUCTINFO_DIR_FILENAME,0);
}
/*调用end*/
//创建文件并写入内容
/**
*
* @param file File to be executed
* @param string File written information
* @throws Exception
*/
private static void write(File file,String string) throws Exception {
Log.d(TAG, "file exists getSit(context) = " + file.exists());
if (!file.exists()) {
ro_build_display_id = BidiFormatter.getInstance().unicodeWrap(Build.DISPLAY);
Log.d(TAG, "Prepare write version ");
FileWriter command = new FileWriter(file);
try {
Log.d(TAG, "Write version");
command.write(string);
command.write("\n");
} catch (IOException e) {
Log.e(TAG, "Write version fail!");
} finally {
Log.d(TAG, "Write version close.");
command.close();
}
}
}
//创建文件并写入内容end
//创建文件并追加内容start
/**
*
* @param fileName File full path
* @param string What needs to be added
*/
public static void addWrite(String fileName, String string) {
FileWriter writer = null;
try {
// Additional version number
writer = new FileWriter(fileName, true);
writer.write(string);
writer.write("\n");
} catch (IOException e) {
Log.e(TAG, "addWrite version fail!");
e.printStackTrace();
} finally {
try {
if(writer != null){
Log.d(TAG, "addWrite version close!");
writer.close();
}
} catch (IOException e) {
Log.e(TAG, "addWrite version close fail!");
e.printStackTrace();
}
}
}
//创建文件并追加内容end
//读取文件内容(一行读取内容) start
/**
*
* @param fileName File full path
*/
public static void readFileByLines(String fileName) {
//Read the current version number
File file = new File(fileName);
BufferedReader reader = null;
try {
Log.d(TAG, "Read file content in line units, one line at a time");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
Log.d(TAG, "line==" +line +",tempString===" +tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//读取文件内容(一行读取内容) end
//修改文件内容start
/**
*
* @param path File full path
* @param string Modified new content
* @return if true Write success , if false Write faile
*
*/
private static boolean modifyFile(String path,int string) {
PrintStream stream=null;
try {
stream=new PrintStream(path);
stream.print(string);
android.util.Log.d(TAG,"Write version success!");
return true;
} catch (FileNotFoundException e) {
android.util.Log.d(TAG,"Write version faile!");
e.printStackTrace();
} finally {
Log.d(TAG, "Write version close.");
stream.close();
}
return false;
}
//修改文件内容end
/*Base64 加密*/
/**
*
* @param str What needs to be encrypted
* @return Encrypted content
* @throws UnsupportedEncodingException
*/
public String encryption(String str) throws UnsupportedEncodingException {
final Base64.Encoder encoder = Base64.getEncoder();
final byte[] strByte;
strByte = str.getBytes("UTF-8");
final String encodedStr = encoder.encodeToString(strByte);
return encodedStr;
}
/*Base64 解密*/
/**
*
* @param str What needs to be decrypted
* @return Decrypted content
* @throws UnsupportedEncodingException
*/
public String decoption(String str) throws UnsupportedEncodingException {
byte[] strByte = str.getBytes("UTF-8");
byte[] strBytedecode = Base64.getDecoder().decode(strByte);
String decodeStr = new String(strBytedecode);
return decodeStr;
}
}
对文件执行操作的方法Utile类笔记
最新推荐文章于 2023-12-27 17:36:06 发布