类名重构,简化代码
This commit is contained in:
		@@ -1,8 +1,8 @@
 | 
				
			|||||||
#Created by .winboll/winboll_app_build.gradle
 | 
					#Created by .winboll/winboll_app_build.gradle
 | 
				
			||||||
#Sun Feb 09 13:09:42 HKT 2025
 | 
					#Sun Feb 09 07:03:25 GMT 2025
 | 
				
			||||||
stageCount=3
 | 
					stageCount=3
 | 
				
			||||||
libraryProject=libappbase
 | 
					libraryProject=libappbase
 | 
				
			||||||
baseVersion=1.2
 | 
					baseVersion=1.2
 | 
				
			||||||
publishVersion=1.2.2
 | 
					publishVersion=1.2.2
 | 
				
			||||||
buildCount=0
 | 
					buildCount=1
 | 
				
			||||||
baseBetaVersion=1.2.3
 | 
					baseBetaVersion=1.2.3
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,8 @@
 | 
				
			|||||||
#Created by .winboll/winboll_app_build.gradle
 | 
					#Created by .winboll/winboll_app_build.gradle
 | 
				
			||||||
#Sun Feb 09 13:09:33 HKT 2025
 | 
					#Sun Feb 09 07:03:25 GMT 2025
 | 
				
			||||||
stageCount=3
 | 
					stageCount=3
 | 
				
			||||||
libraryProject=libappbase
 | 
					libraryProject=libappbase
 | 
				
			||||||
baseVersion=1.2
 | 
					baseVersion=1.2
 | 
				
			||||||
publishVersion=1.2.2
 | 
					publishVersion=1.2.2
 | 
				
			||||||
buildCount=0
 | 
					buildCount=1
 | 
				
			||||||
baseBetaVersion=1.2.3
 | 
					baseBetaVersion=1.2.3
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,7 +53,7 @@ public abstract class BaseBean<T extends BaseBean> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            T beanTemp = clazz.newInstance();
 | 
					            T beanTemp = clazz.newInstance();
 | 
				
			||||||
            String szBeanSimpleName = beanTemp.getName();
 | 
					            String szBeanSimpleName = beanTemp.getName();
 | 
				
			||||||
            String szListJson = FileUtils.readStringFromFile(szFilePath);
 | 
					            String szListJson = UTF8FileUtils.readStringFromFile(szFilePath);
 | 
				
			||||||
            StringReader stringReader = new StringReader(szListJson);
 | 
					            StringReader stringReader = new StringReader(szListJson);
 | 
				
			||||||
            JsonReader jsonReader = new JsonReader(stringReader);
 | 
					            JsonReader jsonReader = new JsonReader(stringReader);
 | 
				
			||||||
            jsonReader.beginArray();
 | 
					            jsonReader.beginArray();
 | 
				
			||||||
@@ -204,7 +204,7 @@ public abstract class BaseBean<T extends BaseBean> {
 | 
				
			|||||||
            try {
 | 
					            try {
 | 
				
			||||||
                File fTemp = new File(szFilePath);
 | 
					                File fTemp = new File(szFilePath);
 | 
				
			||||||
                if (fTemp.exists()) {
 | 
					                if (fTemp.exists()) {
 | 
				
			||||||
                    T beanTemp = clazz.newInstance();String szJson = FileUtils.readStringFromFile(szFilePath);
 | 
					                    T beanTemp = clazz.newInstance();String szJson = UTF8FileUtils.readStringFromFile(szFilePath);
 | 
				
			||||||
                    return beanTemp.parseStringToBean(szJson, clazz);
 | 
					                    return beanTemp.parseStringToBean(szJson, clazz);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } catch (InstantiationException e) {
 | 
					            } catch (InstantiationException e) {
 | 
				
			||||||
@@ -226,7 +226,7 @@ public abstract class BaseBean<T extends BaseBean> {
 | 
				
			|||||||
    public static <T extends BaseBean> boolean saveBeanToFile(String szFilePath, T bean) {
 | 
					    public static <T extends BaseBean> boolean saveBeanToFile(String szFilePath, T bean) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            String szJson = bean.toString();
 | 
					            String szJson = bean.toString();
 | 
				
			||||||
            FileUtils.writeStringToFile(szFilePath, szJson);
 | 
					            UTF8FileUtils.writeStringToFile(szFilePath, szJson);
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
 | 
					            LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
 | 
				
			||||||
@@ -248,7 +248,7 @@ public abstract class BaseBean<T extends BaseBean> {
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            File fTemp = new File(szFilePath);
 | 
					            File fTemp = new File(szFilePath);
 | 
				
			||||||
            if (fTemp.exists()) {
 | 
					            if (fTemp.exists()) {
 | 
				
			||||||
                String szListJson = FileUtils.readStringFromFile(szFilePath);
 | 
					                String szListJson = UTF8FileUtils.readStringFromFile(szFilePath);
 | 
				
			||||||
                return parseStringToBeanList(szListJson, beanList, clazz);
 | 
					                return parseStringToBeanList(szListJson, beanList, clazz);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
@@ -272,7 +272,7 @@ public abstract class BaseBean<T extends BaseBean> {
 | 
				
			|||||||
    public static <T extends BaseBean> boolean saveBeanListToFile(String szFilePath, ArrayList<T> beanList) {
 | 
					    public static <T extends BaseBean> boolean saveBeanListToFile(String szFilePath, ArrayList<T> beanList) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            String szJson = toStringByBeanList(beanList);
 | 
					            String szJson = toStringByBeanList(beanList);
 | 
				
			||||||
            FileUtils.writeStringToFile(szFilePath, szJson);
 | 
					            UTF8FileUtils.writeStringToFile(szFilePath, szJson);
 | 
				
			||||||
            //LogUtils.d(TAG, "FileUtil.writeFile beanList.size() is " + Integer.toString(beanList.size()));
 | 
					            //LogUtils.d(TAG, "FileUtil.writeFile beanList.size() is " + Integer.toString(beanList.size()));
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,120 +0,0 @@
 | 
				
			|||||||
package cc.winboll.studio.libappbase;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * @Author ZhanGSKen@QQ.COM
 | 
					 | 
				
			||||||
 * @Date 2024/07/19 14:30:57
 | 
					 | 
				
			||||||
 * @Describe 文件工具类
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
import android.content.Context;
 | 
					 | 
				
			||||||
import android.content.res.AssetManager;
 | 
					 | 
				
			||||||
import java.io.File;
 | 
					 | 
				
			||||||
import java.io.FileInputStream;
 | 
					 | 
				
			||||||
import java.io.FileOutputStream;
 | 
					 | 
				
			||||||
import java.io.IOException;
 | 
					 | 
				
			||||||
import java.io.InputStream;
 | 
					 | 
				
			||||||
import java.io.InputStreamReader;
 | 
					 | 
				
			||||||
import java.io.OutputStream;
 | 
					 | 
				
			||||||
import java.io.OutputStreamWriter;
 | 
					 | 
				
			||||||
import java.nio.charset.StandardCharsets;
 | 
					 | 
				
			||||||
import java.nio.file.Files;
 | 
					 | 
				
			||||||
import java.nio.file.Path;
 | 
					 | 
				
			||||||
import java.nio.file.Paths;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
public class FileUtils {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static final String TAG = "FileUtils";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static void copyAssetsToSD(Context context, String szSrcAssets, String szDstSD) {
 | 
					 | 
				
			||||||
        LogUtils.d(TAG, "copyAssetsToSD [" + szSrcAssets + "] to [" + szDstSD + "]");
 | 
					 | 
				
			||||||
        AssetManager assetManager = context.getAssets();
 | 
					 | 
				
			||||||
        InputStream inputStream = null;
 | 
					 | 
				
			||||||
        OutputStream outputStream = null;
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            inputStream = assetManager.open(szSrcAssets);
 | 
					 | 
				
			||||||
            File outputFile = new File(szDstSD);
 | 
					 | 
				
			||||||
            outputStream = new FileOutputStream(outputFile);
 | 
					 | 
				
			||||||
            byte[] buffer = new byte[1024];
 | 
					 | 
				
			||||||
            int length = 0;
 | 
					 | 
				
			||||||
            while ((length = inputStream.read(buffer)) > 0) {
 | 
					 | 
				
			||||||
                outputStream.write(buffer, 0, length);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            outputStream.flush();
 | 
					 | 
				
			||||||
            LogUtils.d(TAG, "copyAssetsToSD done.");
 | 
					 | 
				
			||||||
        } catch (IOException e) {
 | 
					 | 
				
			||||||
            LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
 | 
					 | 
				
			||||||
        } finally {
 | 
					 | 
				
			||||||
            if (inputStream != null) {
 | 
					 | 
				
			||||||
                try {
 | 
					 | 
				
			||||||
                    inputStream.close();
 | 
					 | 
				
			||||||
                } catch (IOException e) {
 | 
					 | 
				
			||||||
                    LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if (outputStream != null) {
 | 
					 | 
				
			||||||
                try {
 | 
					 | 
				
			||||||
                    outputStream.close();
 | 
					 | 
				
			||||||
                } catch (IOException e) {
 | 
					 | 
				
			||||||
                    LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //
 | 
					 | 
				
			||||||
    // 把字符串写入文件,指定 UTF-8 编码
 | 
					 | 
				
			||||||
    //
 | 
					 | 
				
			||||||
    public static void writeStringToFile(String szFilePath, String szContent) throws IOException {
 | 
					 | 
				
			||||||
        File file = new File(szFilePath);
 | 
					 | 
				
			||||||
        if (!file.getParentFile().exists()) {
 | 
					 | 
				
			||||||
            file.getParentFile().mkdirs();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        FileOutputStream outputStream = new FileOutputStream(file);
 | 
					 | 
				
			||||||
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
 | 
					 | 
				
			||||||
        writer.write(szContent);
 | 
					 | 
				
			||||||
        writer.close();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //
 | 
					 | 
				
			||||||
    // 读取文件到字符串,指定 UTF-8 编码
 | 
					 | 
				
			||||||
    //
 | 
					 | 
				
			||||||
    public static String readStringFromFile(String szFilePath) throws IOException {
 | 
					 | 
				
			||||||
        File file = new File(szFilePath);
 | 
					 | 
				
			||||||
        FileInputStream inputStream = new FileInputStream(file);
 | 
					 | 
				
			||||||
        InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
 | 
					 | 
				
			||||||
        StringBuilder content = new StringBuilder();
 | 
					 | 
				
			||||||
        int character;
 | 
					 | 
				
			||||||
        while ((character = reader.read()) != -1) {
 | 
					 | 
				
			||||||
            content.append((char) character);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        reader.close();
 | 
					 | 
				
			||||||
        return content.toString();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static boolean copyFile(File srcFile, File dstFile) {
 | 
					 | 
				
			||||||
        if (!srcFile.exists()) {
 | 
					 | 
				
			||||||
            LogUtils.d(TAG, "The original file does not exist.");
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                // 源文件路径
 | 
					 | 
				
			||||||
                Path sourcePath = Paths.get(srcFile.getPath());
 | 
					 | 
				
			||||||
                // 目标文件路径
 | 
					 | 
				
			||||||
                Path destPath = Paths.get(dstFile.getPath());
 | 
					 | 
				
			||||||
                // 建立目标父级文件夹
 | 
					 | 
				
			||||||
                if (!dstFile.getParentFile().exists()) {
 | 
					 | 
				
			||||||
                    dstFile.getParentFile().mkdirs();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                // 删除旧的目标文件
 | 
					 | 
				
			||||||
                if (dstFile.exists()) {
 | 
					 | 
				
			||||||
                    dstFile.delete();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                // 拷贝文件
 | 
					 | 
				
			||||||
                Files.copy(sourcePath, destPath);
 | 
					 | 
				
			||||||
                LogUtils.d(TAG, "File copy successfully.");
 | 
					 | 
				
			||||||
                return true;
 | 
					 | 
				
			||||||
            } catch (Exception e) {
 | 
					 | 
				
			||||||
                LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -360,7 +360,7 @@ public class LogUtils {
 | 
				
			|||||||
    public static void cleanLog() {
 | 
					    public static void cleanLog() {
 | 
				
			||||||
        if (_mfLogCatchFile.exists()) {
 | 
					        if (_mfLogCatchFile.exists()) {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                FileUtils.writeStringToFile(_mfLogCatchFile.getPath(), "");
 | 
					                UTF8FileUtils.writeStringToFile(_mfLogCatchFile.getPath(), "");
 | 
				
			||||||
                //LogUtils.d(TAG, "cleanLog");
 | 
					                //LogUtils.d(TAG, "cleanLog");
 | 
				
			||||||
            } catch (IOException e) {
 | 
					            } catch (IOException e) {
 | 
				
			||||||
                LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
 | 
					                LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					package cc.winboll.studio.libappbase;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @Author ZhanGSKen@QQ.COM
 | 
				
			||||||
 | 
					 * @Date 2024/07/19 14:30:57
 | 
				
			||||||
 | 
					 * @Describe UTF-8编码文件工具类
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					import java.io.FileInputStream;
 | 
				
			||||||
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.InputStreamReader;
 | 
				
			||||||
 | 
					import java.io.OutputStreamWriter;
 | 
				
			||||||
 | 
					import java.nio.charset.StandardCharsets;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class UTF8FileUtils {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static final String TAG = "FileUtils";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //
 | 
				
			||||||
 | 
					    // 把字符串写入文件,指定 UTF-8 编码
 | 
				
			||||||
 | 
					    //
 | 
				
			||||||
 | 
					    public static void writeStringToFile(String szFilePath, String szContent) throws IOException {
 | 
				
			||||||
 | 
					        File file = new File(szFilePath);
 | 
				
			||||||
 | 
					        if (!file.getParentFile().exists()) {
 | 
				
			||||||
 | 
					            file.getParentFile().mkdirs();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        FileOutputStream outputStream = new FileOutputStream(file);
 | 
				
			||||||
 | 
					        OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
 | 
				
			||||||
 | 
					        writer.write(szContent);
 | 
				
			||||||
 | 
					        writer.close();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //
 | 
				
			||||||
 | 
					    // 读取文件到字符串,指定 UTF-8 编码
 | 
				
			||||||
 | 
					    //
 | 
				
			||||||
 | 
					    public static String readStringFromFile(String szFilePath) throws IOException {
 | 
				
			||||||
 | 
					        File file = new File(szFilePath);
 | 
				
			||||||
 | 
					        FileInputStream inputStream = new FileInputStream(file);
 | 
				
			||||||
 | 
					        InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
 | 
				
			||||||
 | 
					        StringBuilder content = new StringBuilder();
 | 
				
			||||||
 | 
					        int character;
 | 
				
			||||||
 | 
					        while ((character = reader.read()) != -1) {
 | 
				
			||||||
 | 
					            content.append((char) character);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        reader.close();
 | 
				
			||||||
 | 
					        return content.toString();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user