Java 逐块读写文件
我正在开发一个文件加密程序。我正在使用下面的函数加密文件 直到我意识到它不适合大的;因为它将所有文件内容读入内存。现在,我需要创建一个可以读写文件内容的函数。我该怎么做Java 逐块读写文件,java,android,file,kotlin,Java,Android,File,Kotlin,我正在开发一个文件加密程序。我正在使用下面的函数加密文件 直到我意识到它不适合大的;因为它将所有文件内容读入内存。现在,我需要创建一个可以读写文件内容的函数。我该怎么做 private fun encryptFile(file: File) { val originalData = file.readBytes() val encryptData = encrypt(originalData) encryptData?.run { file.writeBytes(
private fun encryptFile(file: File) {
val originalData = file.readBytes()
val encryptData = encrypt(originalData)
encryptData?.run {
file.writeBytes(this)
}
}
看看代码
编辑:使用Chilkat库怎么样
用于加密文件块的代码示例
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
CkCrypt2 crypt = new CkCrypt2();
crypt.put_CryptAlgorithm("aes");
crypt.put_CipherMode("cbc");
crypt.put_KeyLength(128);
crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex");
crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex");
String fileToEncrypt = "qa_data/hamlet.xml";
CkFileAccess facIn = new CkFileAccess();
boolean success = facIn.OpenForRead(fileToEncrypt);
if (success != true) {
System.out.println("Failed to open file that is to be encrytped.");
return;
}
String outputEncryptedFile = "qa_output/hamlet.enc";
CkFileAccess facOutEnc = new CkFileAccess();
success = facOutEnc.OpenForWrite(outputEncryptedFile);
if (success != true) {
System.out.println("Failed to encrypted output file.");
return;
}
// Let's encrypt in 10000 byte chunks.
int chunkSize = 10000;
int numChunks = facIn.GetNumBlocks(chunkSize);
crypt.put_FirstChunk(true);
crypt.put_LastChunk(false);
CkBinData bd = new CkBinData();
int i = 0;
while (i < numChunks) {
i = i+1;
if (i == numChunks) {
crypt.put_LastChunk(true);
}
// Read the next chunk from the file.
// The last chunk will be whatever amount remains in the file..
bd.Clear();
facIn.FileReadBd(chunkSize,bd);
// Encrypt.
crypt.EncryptBd(bd);
// Write the encrypted chunk to the output file.
facOutEnc.FileWriteBd(bd,0,0);
crypt.put_FirstChunk(false);
}
// Make sure both FirstChunk and LastChunk are restored to true after
// encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions
// will produce unexpected results.
crypt.put_FirstChunk(true);
crypt.put_LastChunk(true);
facIn.FileClose();
facOutEnc.FileClose();
// Decrypt the encrypted output file in a single call using CBC mode:
String decryptedFile = "qa_output/hamlet_dec.xml";
success = crypt.CkDecryptFile(outputEncryptedFile,decryptedFile);
// Assume success for the example..
// Compare the contents of the decrypted file with the original file:
boolean bSame = facIn.FileContentsEqual(fileToEncrypt,decryptedFile);
System.out.println("bSame = " + bSame);
}
}
import com.chilkatsoft.*;
公共类ChilkatExample{
静止的{
试一试{
系统加载库(“chilkat”);
}捕获(未满足链接错误e){
System.err.println(“本机代码库加载失败。\n”+e);
系统出口(1);
}
}
公共静态void main(字符串argv[])
{
CkCrypt2 crypt=新的CkCrypt2();
密码算法(aes);
密码(cbc);
密码长度(128);
密码SetEncodedKey(“000102030405060708090A0B0C0D0E0F”,“十六进制”);
密码SetEncodedIV(“000102030405060708090A0B0C0D0E0F”,“十六进制”);
字符串fileToEncrypt=“qa_data/hamlet.xml”;
CkFileAccess facIn=新的CkFileAccess();
boolean success=facIn.OpenForRead(fileToEncrypt);
如果(成功!=true){
System.out.println(“未能打开要加密的文件”);
返回;
}
字符串outputEncryptedFile=“qa_output/hamlet.enc”;
CkFileAccess facOutEnc=新的CkFileAccess();
success=facOutEnc.OpenForWrite(outputenecryptedfile);
如果(成功!=true){
System.out.println(“未能加密输出文件”);
返回;
}
//让我们加密10000字节的块。
int chunkSize=10000;
int numChunks=facIn.GetNumBlocks(chunkSize);
crypt.put_FirstChunk(true);
crypt.put_LastChunk(false);
CkBinData bd=新的CkBinData();
int i=0;
而(我
看看代码
编辑:使用Chilkat库怎么样
用于加密文件块的代码示例
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
CkCrypt2 crypt = new CkCrypt2();
crypt.put_CryptAlgorithm("aes");
crypt.put_CipherMode("cbc");
crypt.put_KeyLength(128);
crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex");
crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex");
String fileToEncrypt = "qa_data/hamlet.xml";
CkFileAccess facIn = new CkFileAccess();
boolean success = facIn.OpenForRead(fileToEncrypt);
if (success != true) {
System.out.println("Failed to open file that is to be encrytped.");
return;
}
String outputEncryptedFile = "qa_output/hamlet.enc";
CkFileAccess facOutEnc = new CkFileAccess();
success = facOutEnc.OpenForWrite(outputEncryptedFile);
if (success != true) {
System.out.println("Failed to encrypted output file.");
return;
}
// Let's encrypt in 10000 byte chunks.
int chunkSize = 10000;
int numChunks = facIn.GetNumBlocks(chunkSize);
crypt.put_FirstChunk(true);
crypt.put_LastChunk(false);
CkBinData bd = new CkBinData();
int i = 0;
while (i < numChunks) {
i = i+1;
if (i == numChunks) {
crypt.put_LastChunk(true);
}
// Read the next chunk from the file.
// The last chunk will be whatever amount remains in the file..
bd.Clear();
facIn.FileReadBd(chunkSize,bd);
// Encrypt.
crypt.EncryptBd(bd);
// Write the encrypted chunk to the output file.
facOutEnc.FileWriteBd(bd,0,0);
crypt.put_FirstChunk(false);
}
// Make sure both FirstChunk and LastChunk are restored to true after
// encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions
// will produce unexpected results.
crypt.put_FirstChunk(true);
crypt.put_LastChunk(true);
facIn.FileClose();
facOutEnc.FileClose();
// Decrypt the encrypted output file in a single call using CBC mode:
String decryptedFile = "qa_output/hamlet_dec.xml";
success = crypt.CkDecryptFile(outputEncryptedFile,decryptedFile);
// Assume success for the example..
// Compare the contents of the decrypted file with the original file:
boolean bSame = facIn.FileContentsEqual(fileToEncrypt,decryptedFile);
System.out.println("bSame = " + bSame);
}
}
import com.chilkatsoft.*;
公共类ChilkatExample{
静止的{
试一试{
系统加载库(“chilkat”);
}捕获(未满足链接错误e){
System.err.println(“本机代码库加载失败。\n”+e);
系统出口(1);
}
}
公共静态void main(字符串argv[])
{
CkCrypt2 crypt=新的CkCrypt2();
密码算法(aes);
密码(cbc);
密码长度(128);
密码SetEncodedKey(“000102030405060708090A0B0C0D0E0F”,“十六进制”);
密码SetEncodedIV(“000102030405060708090A0B0C0D0E0F”,“十六进制”);
字符串fileToEncrypt=“qa_data/hamlet.xml”;
CkFileAccess facIn=新的CkFileAccess();
boolean success=facIn.OpenForRead(fileToEncrypt);
如果(成功!=true){
System.out.println(“未能打开要加密的文件”);
返回;
}
字符串outputEncryptedFile=“qa_output/hamlet.enc”;
CkFileAccess facOutEnc=新的CkFileAccess();
success=facOutEnc.OpenForWrite(outputenecryptedfile);
如果(成功!=true){
System.out.println(“未能加密输出文件”);
返回;
}
//让我们加密10000字节的块。
int chunkSize=10000;
int numChunks=facIn.GetNumBlocks(chunkSize);
crypt.put_FirstChunk(true);
crypt.put_LastChunk(false);
CkBinData bd=新的CkBinData();
int i=0;
而(我
您的加密功能显然无法保持这种状态。它必须成为一个封装InputStream或OutputStream的东西,然后它就相当简单了
请注意,手动加密几乎100%保证您会把它搞砸,而且加密流已经存在。你有没有理由重新发明一个轮子,并通过重新发明你不应该做的事情来破坏安全性?你的加密功能显然不能保持这种状态。它必须成为一个封装InputStream或OutputStream的东西,然后它就相当简单了 请注意,手动加密是一种新的加密方式