File Content Encoding/Decoding

Source: Internet
Author: User

File Content Encoding/Decoding

/**
* {#} Base64.java Create on Nov 5, 2008 7:19:56
*
*/
Package com. gren. remotecheck. util;

Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. security. MessageDigest;
Import java. security. NoSuchAlgorithmException;

Import org. apache. log4j. Logger;

Import sun. misc. BASE64Decoder;
Import sun. misc. BASE64Encoder;

Public class FileCode {
Public static String encodeBase64File (String path) throws Exception {
File file = new File (path );
FileInputStream inputFile = new FileInputStream (file );
Byte [] buffer = new byte [(int) file. length ()];
InputFile. read (buffer );
InputFile. close ();
Return new BASE64Encoder (). encode (buffer );
}

Public static void decoderBase64File (String base64Code, String targetPath) throws Exception {
Byte [] buffer = new BASE64Decoder (). decodeBuffer (base64Code );

// For (int I = 0; I <buffer. length; I ++ ){
// If (buffer [I] <0 ){
// Buffer [I] ++ = 256;
//}
//}

File f = new File (targetPath );

If (! F. exists ())
F. createNewFile ();

FileOutputStream out = new FileOutputStream (f );
Out. write (buffer );
Out. close ();
}

Private static String hex (byte [] arr ){
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <arr. length; ++ I ){
Sb. append (Integer. toHexString (arr [I] & 0xFF) | 0x100). substring (1, 3 ));
}
Return sb. toString ();
}

Public static String md5Code (String code ){
String result = "";
Try {
MessageDigest md5 = null;

Md5 = MessageDigest. getInstance ("MD5 ");
Md5.update (code. getBytes ());
Result = hex (md5.digest ());

} Catch (NoSuchAlgorithmException e ){
E. printStackTrace ();
}
Return result;
}

Public static String readFileText (String path) throws Exception {
File file = new File (path );
FileInputStream inputFile = new FileInputStream (file );

BufferedReader bf = new BufferedReader (new InputStreamReader (inputFile, "GBK "));

StringBuffer sb = new StringBuffer ();

Char [] rData = new char [1024]; // The default read cache value is 1024.
Int len = 0;
While (true ){
Len = bf. read (rData );
If (len <= 0 ){
// Log.info ("the received data length is 0 ");
Break;
}
Sb. append (new String (rData, 0, len ));
If (len <1024 ){
Break;
}
}

Bf. close ();
InputFile. close ();
Return sb. toString ();
}

Public static void saveTextFile (String filepath, String filetext) throws Exception {

File f = new File (filepath );

If (! F. exists ())
F. createNewFile ();

FileOutputStream out = new FileOutputStream (f );

BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (out, "GBK "));

Bw. write (filetext );

Bw. flush ();

Out. close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.