Java android 文件输入流文件输出流问题

标签 java android fileinputstream fileoutputstream

我需要一个小问题。如何修复该代码以便我也可以在 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"));

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

关于Java android 文件输入流文件输出流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873325/

相关文章:

Android - 在 onBackPressed() 单击时设置动画 - 不起作用

java - GZIPInputStream 可以与 FileInputStream 配合使用,但不能与 InputStream 配合使用

java - 设置主目录或工作目录

java - Android 根据预定义值确定方向和屏幕尺寸

Java密封对象

android - 如何在 map fragment 上绘制从当前位置到用户输入位置的路径?

android - 无法在搜索结果的详细 Activity 中显示图像(来自 JSON)?

java - 如何创建一个全局 FileInputStream 对象,该对象可以从我的项目中的其他类访问?

java - 缓存文件输入流

java - 如何将一个对象深度复制到另一个具有相同字段的类对象?