`

base64转码

阅读更多
package cn.ehoo.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import sun.misc.BASE64Decoder;
/*
 * and open the template in the editor.
 */

/**
 * 
 * @author Administrator
 */
public class Base64 {

	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}



	private static synchronized void printToFile(String msg, String fileName) {
		BufferedWriter mBufWriter = null;
		try {
			if (!createFile(fileName))
				return;
			FileWriter fileWriter = new FileWriter(fileName, true);
			mBufWriter = new BufferedWriter(fileWriter);
			mBufWriter.write(msg);
			mBufWriter.newLine();
			mBufWriter.flush();
			mBufWriter.close();
		} catch (Throwable e) {
			try {
				mBufWriter.close();
			} catch (Throwable t) {
			}
		}
		return;
	}

	private static boolean createFile(String fileName) throws IOException, Exception {
		File file = new File(fileName);
		if (file.exists()) {
			if (!file.canWrite())
				return false;
		} else {
			String path = null;
			int firstSlash = fileName.indexOf(File.separatorChar);
			int finalSlash = fileName.lastIndexOf(File.separatorChar);
			if (finalSlash != 0)
				if (finalSlash == 1)
					path = File.separator;
				else if (firstSlash == finalSlash)
					path = fileName.substring(0, finalSlash + 1);
				else
					path = fileName.substring(0, finalSlash);
			File dir = new File(path);
			dir.mkdirs();
		}
		return true;
	}
	
	public static void main(String[] args) throws Exception {
		String encoding = "UTF-8"; // 字符编码
		InputStreamReader in = new InputStreamReader(new FileInputStream(new File("whp2.txt")), encoding);
		BufferedReader bufferedReader = new BufferedReader(in);
		String lineTXT = null;
		while ((lineTXT = bufferedReader.readLine()) != null) {
			printToFile(getFromBASE64(lineTXT), "11.txt");
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics