怎么对文件进行加密解密,我的速度太低了,要好几秒.各位大侠帮帮忙,来者有分
输入一个密码,对文件进行简单的加密,再保存,可以输入密码再解密
public static String readString(String fileName, Boolean bEn) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); if (bEn) { while (content != null) { content = bf.readLine(); String str = ""; for (int i = 0; i < content.length(); i++) { str += content.charAt(i) + 3; } content = str; if (content == null) { break; } content = str; sb.append(content);// .trim()); } } else { while (content != null) { content = bf.readLine(); String str = ""; for (int i = 0; i < content.length(); i++) { str += content.charAt(i) - 3; } if (content == null) { break; } sb.append(content);// .trim()); } } bf.close(); fileOperation.writeBytes(fileName, sb.toString()); return sb.toString(); } public static int writeBytes(String file, String string) { java.io.ObjectOutputStream out; try { out = new java.io.ObjectOutputStream(new java.io.FileOutputStream( file)); out.writeObject(string); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return -1; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return -2; } return 0; }
while ((content = bf.readLine()) != null) { for (int i = 0; i < content.length(); i++) { char c = content.charAt(i) + 3; sb.append(c); // 既然用了StringBuffer/Builder,就绝对绝对不要str += xxx + xxx + xxx; sb.append(str); }}
[解决办法]
貌似你这里,对换行有遗漏
[解决办法]
你都读到内存上操作,要考虑大文件的时候的性能问题
建议还是读一行写一行,写到一个临时文件,最后再把临时文件改名
感觉你的代码要很多累赘的处理,还有,如果要对字符处理,最好转成字符数组来操作,charAt的性能比较低
public static String readString(String fileName, Boolean bEn) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); if (bEn) { while (content != null) { content = bf.readLine(); //String str = ""; //for (int i = 0; i < content.length(); i++) { // str += content.charAt(i) + 3; //} for (char c : content.toCharArray()) { sb.append(c+3); } //content = str; //if (content == null) { // break; //} //content = str; //sb.append(content);// .trim()); } } else { while (content != null) { content = bf.readLine(); //String str = ""; //for (int i = 0; i < content.length(); i++) { // str += content.charAt(i) - 3; //} for (char c : content.toCharArray()) { sb.append(c-3); } //if (content == null) { // break; //} //sb.append(content);// .trim()); } } bf.close(); fileOperation.writeBytes(fileName, sb.toString()); return sb.toString(); } public static int writeBytes(String file, String string) { java.io.ObjectOutputStream out; try { out = new java.io.ObjectOutputStream(new java.io.FileOutputStream( file)); out.writeObject(string); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return -1; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return -2; } return 0; }
[解决办法]
勘误:
1 StringBuilder sb = new StringBuilder((int)file.size());
2 阿宝的while 判断改错了,最后一行会NullPointerException的。
[解决办法]
在LZ的基础上改的
把while改成
while((content=br.readLine()) != null)
while循环体里的content = br.readLine()行去掉
[解决办法]
package prx;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Test { public void encryptOrDecryptFile(File file) throws IOException { //临时存储加密数据的文件 File tempFile = new File(file.getAbsolutePath() + ".tmp"); InputStream is = new FileInputStream(file); OutputStream os = new FileOutputStream(tempFile); //数据缓冲区 byte[] buff = new byte[8192]; while(is.read(buff) != -1) { for(int i=0; i<buff.length; i++) { buff[i] = (byte) (buff[i] ^ 0xffffffff); //异或加密,两次异或将得到原数据:即第一次运行后位加密,再次运行后则解密 } os.write(buff); //同步写入一个缓冲区的数据 } is.close(); os.close(); //删除原文件 file.delete(); //修改临时文件名位原文件名 tempFile.renameTo(file); } public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); File file = new File("f:/test.txt"); Test test = new Test(); test.encryptOrDecryptFile(file); System.out.println(System.currentTimeMillis() - start); }}
[解决办法]
public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("c:/data1.txt"); OutputStream os = new FileOutputStream("c:/data2.txt"); int key = 333333; byte[] buffer = new byte[4096]; int len = 0; while ((len = is.read(buffer)) != -1) { for (int i = 0; i < len; i++) { buffer[i] = (byte) (buffer[i] & 0xff ^ key); } os.write(buffer, 0, len); } os.flush(); os.close(); is.close(); }
[解决办法]