位运算的效率改善问题

nyfair 2008-02-11 11:25:55
import java.io.*;

public class Temp {
public static void main(String[] input) throws Exception {
int buffer,key;
FileInputStream fin = new FileInputStream(input[0]);
FileOutputStream fout = new FileOutputStream(input[0]+".tmp");
buffer = fin.read();
key = buffer^0x89;
fout.write((char)0x89);
while (fin.available() != 0) {
buffer = fin.read();
fout.write((char)(buffer^key));
}
}
}

感觉自己写的很傻,转换大文件时速度很慢
可是用Buffered包装后发现写出来的文件会尾部缺失
...全文
79 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nyfair 2008-02-11
  • 打赏
  • 举报
回复
谢谢,非常受教啊
老紫竹 2008-02-11
  • 打赏
  • 举报
回复
你的速度慢,是因为你的读取和写入速度慢,呵呵!

请参考
import java.io.*;

public class Temp {
public static void main(String[] input) throws Exception {
int buffer, key;
FileInputStream fin = new FileInputStream(input[0]);
FileOutputStream fout = new FileOutputStream(input[0] + ".tmp");
buffer = fin.read();
key = buffer ^ 0x89;
fout.write((char) 0x89);
byte[] bs = new byte[10240]; // 用一个10k的缓冲区不就行了,如果怕占用内存,1-2K也可以
int len = -1;

while ((len = fin.read(bs)) != 01) {
for (int i = 0; i < len; i++) {
bs[i] ^= key;
}
fout.write(bs, 0, len);
}
// 其他的代码,比如关闭流
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧