繁体   English   中英

Java Android FileInputStream FileOutputStream问题

[英]Java android fileinputstream fileoutputstream issue

我需要一个小问题。如何修复该代码,以便我也可以在android中使用它。我只需要从android projet的Assets文件夹中加载文件,解密并显示文件的大小以及需要多长时间给应用程序解密。

代码:

package decryption;

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;

    public class Decryption {
      public static void main(String args[]) throws Exception {


          File file = new File("ecryption.pdf");
          System.out.println(file.getAbsolutePath());
          System.out.println("user.dir is: " + System.getProperty("user.dir"));

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        FileInputStream fis   = new FileInputStream(new File("ecrypted.pdf"));
        long start = System.currentTimeMillis();
        System.out.print(start+"           ");
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        FileOutputStream fos  = new FileOutputStream(new File("decrypted.pdf"));
        long end = System.currentTimeMillis();
        System.out.print(end);


        byte[] b = new byte[8];
        int i;

        while ((i = cis.read(b)) != -1) {
          fos.write(b, 0, i);
        }
        fos.flush(); fos.close();
        cis.close(); fis.close();       
      }
}
//      File file = new File("ecryption.pdf");
//      System.out.println(file.getAbsolutePath());
//      System.out.println("user.dir is: " + System.getProperty("user.dir"));

// FileInputStream fis   = new FileInputStream(new File("ecrypted.pdf"));
InputStream fis = getAssets().open("ecryption.pdf");

// FileOutputStream fos  = new FileOutputStream(new File("decrypted.pdf"));
FileOutputStream fos  = new FileOutputStream(
       new File(Environment.getExternalStorageDirectory(), "decrypted.pdf"));

然后,您需要编译和调整其余部分。

这将一次复制一个文件...从那里开始。

    public void copyAssets() {

    try {
        in = getAssets().open("aabbccdd.mp3");
        File outFolder = new File(root.getAbsolutePath() + "/testfolder182");
        outFolder.mkdir();
        File outFile = new File(outFolder, "ooooooooohhhigetit.mp3");
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: ", e);
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM