android 文件读取和写入 zip解压

public static String readFile(String storagePath){
   //文件的读取
   File newFile = new File(storagePath);
   if(newFile.exists()){
      String res = "";
      try{
         File file = new File(storagePath);
         FileInputStream fin = new FileInputStream(file);
         //用这个就不行了,必须用FileInputStream
         int length = fin.available();
         byte [] buffer = new byte[length];
         fin.read(buffer);
         res = EncodingUtils.getString(buffer, "UTF-8");
         fin.close();
      }catch(Exception e){
         e.printStackTrace();
      }
      return res;

   }else {
      return null;
   }


}带换行符读取 Android 带换行符文本读取_xiaokakajk的博客-优快云博客
FileUtils.writeFile(path, new Gson().toJson(openDoorBean), false)
String 转 bean
HumitureData humitureData = new Gson().fromJson(FileUtils.readFile(getFilePath()), HumitureData.class);
 Type listType = new TypeToken<List<WeekTimetableBean.DataBean>>() {}.getType();
 String path=ResManager.getInstance().getResRootPath(mContext)+File.separator+"newtimetable/json";
        String timetableList = FileUtil.readFile(path);
      
        try {
            list = new Gson().fromJson(timetableList, listType );
        }catch (IllegalStateException e){
            e.printStackTrace();
        }
FileUtils.writeFile(filepath, new Gson().toJson(weekTimetableBean.getData()), false)
public static boolean writeFile(String filename, String content, boolean append) {
    boolean isSuccess = false;
    BufferedWriter bufferedWriter = null;
    try {
        bufferedWriter = new BufferedWriter(new FileWriter(filename, append));
        bufferedWriter.write(content);
        isSuccess = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(bufferedWriter);
    }
    return isSuccess;
}

 

 /*try {
        String fileName = "/touch_qr.txt";
        String path = Environment.getExternalStorageDirectory()+"/touch";
        ZipFile zf = new ZipFile(path+fileName);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            Log.e("==zip==",entry.getName());
            File desFile = new File(path+"/json");
            upZipFile(zf,entry,desFile);
            String value = FileUtils.readUTF8FromFile(path+"/json");
            Log.e("==zip==",value);
            Intent intentqr = new Intent();
            intentqr.setAction("net.bunnytouch.bunnyad.service.senttemperature_qr");
            intentqr.putExtra("value", value + "");
            QRService.this.sendBroadcast(intentqr);
            QRData QR = new Gson().fromJson(value, QRData.class);
            Log.e("==zip==",QR.getCredentialSubject().getAge());
        }
    }catch (IOException E){

    }*/
    return super.onStartCommand(intent, flags, startId);
}
//解压缩一个文件
private void upZipFile(ZipFile zf, ZipEntry entry, File desFile) throws IOException {
    InputStream in = zf.getInputStream(entry);
    if (!desFile.exists()) {
        File fileParentDir = desFile.getParentFile();
        if (!fileParentDir.exists()) {
            fileParentDir.mkdirs();
        }
        if (!desFile.createNewFile()) {
            Log.e("==zip==", "Create " + desFile.getAbsolutePath() + " failed!");
        }
    }
    OutputStream out = new FileOutputStream(desFile);
    byte buffer[] = new byte[1024 * 1024];
    int realLength;
    while ((realLength = in.read(buffer)) > 0) {
        out.write(buffer, 0, realLength);
    }
    in.close();
    out.close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值