http://www.softexam.cn/tech/details.asp?catalogf=7&catalogs=35&catalogt=0&article_id=10356
http://www.zdnet.com.cn/developer/code/story/0,2000081534,39312959,00.htm
package com.startech.fjtbcj.down;
import java.io.*;
import java.util.*;
import sun.misc.BASE64Decoder;
public class ExchangeFile {
/** ????????????
@param fileName ??????????(e.g. "d:\\1.txt")
@param applyBean ?????????ArrayList
*/
public void transferToTxtFile(String fileName, ArrayList applyBean)
throws IOException {
char token = '$';
StringBuffer sb = new StringBuffer();
BufferedWriter writer =
new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(fileName)));
UserInfo[] UserInfoArray = new UserInfo[applyBean.size()];
for (int i = 0; i < applyBean.size(); i++) {
UserInfoArray[i] = (UserInfo) applyBean.get(i);
sb.append(BASE64.getBASE64(UserInfoArray[i].getPic()));
sb.append(token);
writer.write(sb.toString());
writer.newLine();
writer.flush();
sb.delete(0, sb.length());
}
writer.close();
}
/**???????????
@param fileName ?????????
@return ArrayList ?????????ArrayList
*/
public ArrayList getFromTxtFile(String fileName) throws IOException {
BufferedReader reader = null;
ArrayList aL = new ArrayList();
try{
reader =
new BufferedReader(
new InputStreamReader(new FileInputStream(fileName)));
String str = null;
StringTokenizer token;
ArrayList tokenList = new ArrayList();
aL = new ArrayList();
while ((str = reader.readLine()) != null) {
token = new StringTokenizer(str, "$", false);
while (token.hasMoreTokens()) {
String tempToken = token.nextToken();
tokenList.add(tempToken);
}
UserInfo user = new UserInfo();
user.setPic(BASE64.getFromBASE64((String)tokenList.get(0)));
tokenList.clear();
aL.add(user);
}
reader.close();
}catch(Exception e){reader.close();e.printStackTrace();
}
return aL;
}
// ????
public static void main(String[] args) throws IOException {
System.out.println("test 'transferToTxtFile' input 1 please");
System.out.println("test 'getFromTxtFile' input 2 please");
BufferedReader lineOfText =
new BufferedReader(new InputStreamReader(System.in));
String textLine = lineOfText.readLine();
if (textLine.equals("1")) {
UserInfo user1 = new UserInfo();
FileInputStream file = new FileInputStream("c:\\1.jpg");
byte[] filebyte = new byte[file.available()];
file.read(filebyte);
file.close();
user1.setPic(filebyte);
ArrayList aL = new ArrayList();
aL.add(user1);
ExchangeFile efile = new ExchangeFile();
efile.transferToTxtFile("c:\\test1.dat", aL);
aL.clear();
}
if (textLine.equals("2")) {
ExchangeFile efile = new ExchangeFile();
ArrayList aL = efile.getFromTxtFile("c:\\test1.dat");
for (int i = 0; i < aL.size(); i++) {
FileOutputStream file = new FileOutputStream("d:\\"+ i +"1.jpg");
file.write(((UserInfo) aL.get(i)).getPic());
file.flush();
file.close();
}
}
}
}
//???
class BASE64 {
public static String getBASE64(byte[] bs) {
if (bs == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(bs);
}
public static byte [] getFromBASE64(String str) throws Exception{
if(str==null) return null;
return (new sun.misc.BASE64Decoder()).decodeBuffer(str);
}
}
class UserInfo {
private String userName;
private String userAge;
private byte[] pic;
public void setUserName(String username) {
this.userName = username;
}
public String getUserName() {
return this.userName;
}
public void setUserAge(String age) {
this.userAge = age;
}
public String getUserAge() {
return this.userAge;
}
/**
* @return
*/
public byte[] getPic() {
return pic;
}
/**
* @param bs
*/
public void setPic(byte[] bs) {
pic = bs;
}
}