一个是AssetsUtil.java工具类;另外一个是单元测试代码AssetsUtilTest.java
AssetsUtil.java 代码package com.palmv.networksalebox.util; import android.content.Context; import org.apache.http.util.EncodingUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; /** * @author:czg * @date:2016/9/21 * @description: */ public class AssetsUtil { /** * 从assets目录中复制整个文件夹内容 * * @param context Context 使用CopyFiles类的Activity * @param oldPath String 原文件路径 如:/aa * @param newPath String 复制后路径 如:xx:/bb/cc */ public static void copyFilesFassets(Context context, String oldPath, String newPath) { try { String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名 if (fileNames.length > 0) {//如果是目录 File file = new File(newPath); file.mkdirs();//如果文件夹不存在,则递归 for (String fileName : fileNames) { copyFilesFassets(context, oldPath + "/" + fileName, newPath + "/" + fileName); } } else {//如果是文件 InputStream is = context.getAssets().open(oldPath); FileOutputStream fos = new FileOutputStream(new File(newPath)); byte[] buffer = new byte[1024]; int byteCount = 0; while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节 fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流 } fos.flush();//刷新缓冲区 is.close(); fos.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 从assets 文件夹中获取文件并读取数据 public static String getFromAssets(Context context, String fileName) { String result = ""; try { InputStream in = context.getResources().getAssets().open(fileName); // 获取文件的字节数 int lenght = in.available(); // 创建byte数组 byte[] buffer = new byte[lenght]; // 将文件中的数据读到byte数组中 in.read(buffer); result = EncodingUtils.getString(buffer, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param context * @param path 路径 "./" 或"" 表示Assets的根目录 * @param extName 扩展名"*" 或"" :表示返回所有 ;".apk" 返回apk的文件 * @return */ public static ArrayList<File> getFileList(Context context, boolean debug, String path, String extName) { ArrayList<File> mFileList = new ArrayList<File>(); String[] fileNames; try { fileNames = context.getResources().getAssets().list(path); for (int i = 0; i < fileNames.length; i++) { if (fileNames[i].toLowerCase().endsWith(extName) || extName.equals("*")) { File file = new File(fileNames[i]); mFileList.add(file); if (debug) { System.out.println(fileNames[i]); } } } } catch (IOException e) { e.printStackTrace(); } return mFileList; } public static String[] getFileStrings(Context context, boolean debug, String path, String extName) { String[] fileNames = null; ArrayList<String> fileStrings = new ArrayList<String>(); int count = 0; try { fileNames = context.getResources().getAssets().list(path); for (int i = 0; i < fileNames.length; i++) { if (fileNames[i].toLowerCase().endsWith(extName) || extName.equals("*")) { fileStrings.add(fileNames[i]); count = count + 1; if (debug) { System.out.println(fileNames[i]); } } } } catch (IOException e) { e.printStackTrace(); } return (String[]) fileStrings.toArray(new String[0]); //return fileNames; } public static String[] getSubArray() { String[] strArray1 = new String[]{"D ", "A ", "B ", "C "}; String[] strArray2 = java.util.Arrays.copyOf(strArray1, 2); for (int i = 0; i < strArray2.length; i++) System.out.print(strArray2[i] + ","); System.out.println(""); return strArray2; } }AssetsUtilTest.java 代码
package com.palmv.networksalebox.util; import android.content.Context; import android.content.res.AssetManager; import com.palmv.common.BuildConfig; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.security.cert.CertificateException; import java.util.ArrayList; /** * Created by kimi on 2016/8/22. * 2016-08-25 zjm ApkSingUtil.getSing()修改了行为,文件不存在时抛出异常 * 2016-08-26 zjm 注释掉 Assert.assertEquals("12f35f43d7122da5e50d19cd8d313005", ApkSignUtil.getSign(new File(getClass().getResource("/register.apk").getPath()))); 它在CI无法通过 ,在个人开发环境可以通过 */ @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, packageName = "com.palmv.networksalebox") public class AssetsUtilTest { AssetManager am ; Context context; @Before public void setUp() throws URISyntaxException { //输出日志 ShadowLog.stream = System.out; context = RuntimeEnvironment.application; AssetManager am =RuntimeEnvironment.application.getAssets(); } @Test public void testGetFromAssets() throws IOException, CertificateException { //am.getAssets().list("./"); String fileText = AssetsUtil.getFromAssets(context,"xposed_init"); System.out.println(fileText); Assert.assertEquals("com.palmv.e_marktingmaster.xposed.DeviceHook", fileText); } @Test public void testGetFileList() throws IOException, CertificateException { ArrayList<File> files ; //files = AssetsUtil.getFileList(context,"./" ,""); files = AssetsUtil.getFileList(context,true,"" ,"*"); System.out.println("============"); Assert.assertNotNull(files); files = AssetsUtil.getFileList(context,false,"" ,".apk"); Assert.assertNotNull(files); System.out.println("======string[]格式返回======"); String[] fileStrings = AssetsUtil.getFileStrings(context,true,"" ,".apk"); System.out.println(fileStrings.length); // System.out.println(fileStrings[0]); // System.out.println(fileStrings[1]); Assert.assertNotNull(fileStrings); // System.out.println("============"); // String[] ss = AssetsUtil.getSubArray(); // Assert.assertEquals(2,ss.length); } }