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. 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;
}
}