常用的工具方法

14 篇文章 0 订阅

1.导出android数据库某张表格数据

 

		public static void getTableData(Cursor cursor) {
			//打印出所有列名
			String[] columnNames = cursor.getColumnNames();
			String name = "";
			for(int i = 0;i< columnNames.length;i++) {
				name = name + columnNames[i] + "-----------";				
			}
			System.out.println("@@"+name);
			//导出数据
			String str  = "";
			while (cursor.moveToNext()) {
				
				for (int i = 0; i < cursor.getColumnCount(); i++)
				{
					str = str + columnNames[i]+":: "+cursor.getString(i)+"----";
					
				}
			}
			Log.i("sqkk","str=="+str);
		}


 

 2.判断整个字符串是否全部是中文汉字,不包含中文的标点符号,英文标点,英文字符

	public boolean isAllChinese(String testStr)
	{
		if (testStr.matches("[\\u4E00-\\u9FA5]+ "))//[\\u4E00-\\u9FA5]是中文区间
		{

			return true;
		}
		return false;
	}


 

3.邮箱正则表达式

mail.matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"))

 

 4.文件流的拷贝

	public static File downloadPic4Thumb(InputStream is,String path) { 
		File tempFile = new File(path);
		if(SimpleTool.isFileExist(tempFile)) {
			try {
			FileOutputStream fos = new FileOutputStream(tempFile);
			int length = 0;
			byte[] buffer = new byte[1024];
			
				while((length = is.read(buffer))!= -1){
					fos.write(buffer, 0, length);
				}
				fos.flush();
				fos.close();
				is.close();
				return tempFile;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;

			}
		}else {
			return null;
		}
	}


 5.文件流操作:向日志文件输出日志

知识点:(1).刚开始我以为直接new一个FileWriter,然后使用append()方法追加就OK了,但是发现,append()方法只是说在执行一次写的操作时是追加,流关闭后,再次写入时

发现,它又覆盖了原来的内容。经过查阅,得知,必须使用构造方法FileWirter(File file, boolean append),第二个参数设置为true就OK了。

第二种在文件末尾追加数据的方法是使用RandomAccessFile类,利用seek()方法移动文件指针到末尾。

http://blog.csdn.net/merry3602/article/details/7045515

(2).使用FileWriter时如何换行

 

方法一:append(System.getProperty("line.separator"))//在j2se里面可以,但是放到android里面就无效了

方法二:fw.append("\r\n");//j2se和android里面都可以用

 

public class AppendToFile {
    /    /**
     * B方法追加文件:使用FileWriter
     */
    public static void appendMethodB(String fileName) {
 

  // TODO Auto-generated method stub      File myFile = new File(fileName);   if(SimpleTool.isFileExist(myFile)) {     FileWriter fw = null;    try {     fw = new FileWriter(myFile,true);         fw.append("InputStream has value,but bitmap is null");      fw.append(System.getProperty("line.separator"));      fw.append("InputStream has problems"); 

        } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }finally {     if(fw != null) {      try {       fw.close();      } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }     }    }    

  }

 

}


==========使用FileOutputSream============

	public static File write2Log(String filePath) {
		File tempFile = new File(filePath);
		FileOutputStream fos = null;
		if (SimpleTool.isFileExist(tempFile)) {
			try {
				fos = new FileOutputStream(tempFile,true);
				int length = 0;
				byte[] buffer = "InputStream is null==".getBytes();
				fos.write(buffer);				
				fos.write("\r\n".getBytes());				
				return tempFile;
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				try {
					if(fos != null) {
						
						fos.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			return null;
		}
	}


==========使用RandomAccessFile===================

	public static File writeLog(String filePath) {
		File tempFile = new File(filePath);
		RandomAccessFile raf = null;
		if (SimpleTool.isFileExist(tempFile)) {
			try {
				raf = new RandomAccessFile(tempFile, "rwd");
				int length = 0;
				byte[] buffer = "InputStream is null==".getBytes();
				raf.seek(raf.length());
				raf.write(buffer);
				raf.write("\r\n".getBytes());				
				return tempFile;
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				try {
					if(raf != null) {
						
						raf.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			return null;
		}
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值