您的位置:首页 > 其它

文件读写BASE64解密、加密算法

2011-09-08 18:05 435 查看
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class FileUtils {

public static File saveFile(String fileContent, String filePath)
throws IOException {
if (fileContent == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] picBinary = decoder.decodeBuffer(fileContent);
File file = new File(filePath);
FileOutputStream out = new FileOutputStream(file);
out.write(picBinary);
out.close();
return file;
}

public static String readFile(String filePath) {
try {
sun.misc.BASE64Encoder encoder = new BASE64Encoder();
File file = new File(filePath);
java.io.FileReader reader = new FileReader(file);
char[] c = new char[1024];
// FileInputStream in = new FileInputStream(file);
// byte[] b = new byte[1024];
StringBuffer buf = new StringBuffer();
int index = -1;
while ((index = reader.read(c)) > 0) {
buf.append(c, 0, index);
}
String result = encoder.encode(buf.toString().getBytes());
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}


本文出自 “利他互联litalink.com” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: