功能性代码集合

将一个汉字字符串转成拼音

package com.test.java;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtills {

	/**
	 * 将一个汉字字符串转成拼音,如果字符串中包含非汉字,以""代替
	 * @param word
	 * @return
	 */
	public static String pinYinOf(String word) {

		StringBuilder ret = new StringBuilder();

		String tmp;
		if (null != word) {

			for (int i = 0; i < word.length(); i++) {

				tmp = pingYingOf(word.charAt(i));

				ret.append(tmp);

			}

		}

		return ret.toString();

	}

	private static String pingYingOf(char word) {

		String[] ret = null;

		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

		try {
			ret = PinyinHelper.toHanyuPinyinStringArray(word, format);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (null != ret) {
			return ret[0];
		} else {
			return "";
		}

	}
}


===================================================================================
将一个字符串写入文件,如果这个文件存在,删除原来的内容

 

/**
 * 将一个字符串写入文件,如果这个文件存在,删除原来的内容
 * @param content
 */
	private void writeLogToFile(String content)
	{
		int i = 1;
		String name = "/sdcard/tmp.txt";
		File file = new File(name);
		if(file.exists())
		{
			file.delete();
			try
			{
				file.createNewFile();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		try
		{
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
			bufferedWriter.write(content);
			bufferedWriter.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 

 

 

/**
	打印行数
	*/
		public static void log(String string){     
			StackTraceElement[] stacks= new Throwable().getStackTrace();   
			StringBuffer sb=  new StringBuffer();
	        sb.append("class: " ).append(stacks[1].getClassName()).append("; method: ").append(stacks[1].getMethodName()).append("; number: ").append(stacks[1].getLineNumber()); 	                 
//	        Log.v("kang", "kang:"+sb.toString()+"--------->"+string);
	        System.out.println("MyLog:"+sb.toString());
	        stacks = null;
	        sb = null;
	    }   
		/**
		 * 打印android剩余内存
		 */
//		public static void disPlayMemInfo()
//		{
//			try
//			{
//				InputStream is = new FileInputStream("/proc/meminfo");
//				 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//				 int count = 0;
//				 String totalMem = null;
//				 String freeMem = null;
//				 String   line = reader.readLine();   
//				 while (line != null) { 
//					 
//					 if(line.startsWith("MemTotal"))
//					 {
//						 count++;
//						 totalMem=line.substring(0, line.lastIndexOf(" "));
//						 totalMem = totalMem.substring(totalMem.lastIndexOf(" ")+1,totalMem.length());
//					 }
//					 else if(line.startsWith("MemFree"))
//					 {
//						 count++;
//						 freeMem=line.substring(0, line.lastIndexOf(" "));
//						 freeMem = freeMem.substring(freeMem.lastIndexOf(" ")+1,freeMem.length());
//					 }
//					 if(count == 2)
//					 {
//						 log("memTotal = "+totalMem+",freeMem="+freeMem);
//						 break;
//					 }
//					 line = reader.readLine();
//				 }
//				 is.close();
//			} catch (FileNotFoundException e)
//			{
//				log("error");
//				e.printStackTrace();
//			} catch (IOException e)
//			{
//				log("error");
//				e.printStackTrace();
//			};
//		}

 

 

你可能感兴趣的:(我的流水账,java,移动开发)