对文件执行操作的方法Utile类笔记

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值