本文主要是介绍Android往SD卡写数据范例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前几天,为了监控WIFI开关情况,把有效信息保存在SD,下面是相关代码:
1、读写工具类
package com.android.server;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;import android.content.Context;
import android.os.Environment;
import android.util.Log;public class WifiLogFileUtil
{protected static final String TAG = "WifiLogFileUtil";private static final boolean DBG = true;private static final String FILE_NAME = "/WIFI/wifi.txt";private static final String FILE_PATH = Environment.getExternalStorageDirectory( ).getAbsoluteFile( ) + FILE_NAME;// 0.5Mprivate static final int FILE_SIZE = 1024*512;public static void writeData2Sdcard( Context context, boolean enable, String packageName ){FileWriter fileWriter = null;try{if ( Environment.getExternalStorageState( ).equals( Environment.MEDIA_MOUNTED ) ){File sdFile = new File( FILE_PATH );boolean ret = true;if ( sdFile.exists( ) == false ){ret = createFile( FILE_PATH );}if ( ret ){long length = sdFile.length( );if ( DBG )Log.d( TAG, "目标文件length=" + length );if ( length > FILE_SIZE ){fileWriter = new FileWriter( sdFile, false );}else{fileWriter = new FileWriter( sdFile, true );}SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );Date curDate = new Date( );String buffer = dateFormat.format( curDate );String desBuffer = String.format( "%s %s %s\n", buffer, enable, packageName );fileWriter.write( desBuffer );if ( DBG )Log.d( TAG, "写入内容:" + desBuffer );}}} catch ( Exception exception ){exception.printStackTrace( );} finally{try{if ( fileWriter != null ){fileWriter.close( );}} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}}public static void readDat4Sdcard( Context context ){FileReader fileReader = null;BufferedReader bufferedReader = null;try{if ( Environment.getExternalStorageState( ).equals( Environment.MEDIA_MOUNTED ) ){File sdFile = new File( FILE_PATH );if ( sdFile.exists( ) ){fileReader = new FileReader( sdFile );bufferedReader = new BufferedReader( fileReader );String buffer = bufferedReader.readLine( );StringBuffer stringBuffer = new StringBuffer( );while ( buffer != null ){stringBuffer.append( buffer );buffer = bufferedReader.readLine( );}if ( DBG )Log.d( TAG, "目标文件内容:\n" + stringBuffer );}}} catch ( Exception exception ){exception.printStackTrace( );} finally{if ( bufferedReader != null ){try{bufferedReader.close( );} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}if ( fileReader != null ){try{fileReader.close( );} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}}}public static boolean createFile( String destFileName ){File file = new File( destFileName );if ( file.exists( ) ){if ( DBG )Log.d( TAG, "创建单个文件" + destFileName + "失败,目标文件已存在!" );return false;}if ( destFileName.endsWith( File.separator ) ){if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败,目标文件不能为目录!" );return false;}// 判断目标文件所在的目录是否存在if ( !file.getParentFile( ).exists( ) ){// 如果目标文件所在的目录不存在,则创建父目录if ( DBG )Log.d( TAG, "目标文件所在目录不存在,准备创建它!" );if ( !file.getParentFile( ).mkdirs( ) ){if ( DBG )Log.e( TAG, "创建目标文件所在目录失败!" );return false;}}// 创建目标文件try{if ( file.createNewFile( ) ){if ( DBG )Log.d( TAG, "创建单个文件" + destFileName + "成功!" );return true;}else{if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败!" );return false;}} catch ( IOException e ){e.printStackTrace( );if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败!" + e.getMessage( ) );return false;}}
}
2、调用地方
/*** see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}* @param enable {@code true} to enable, {@code false} to disable.* @return {@code true} if the enable/disable operation was* started or is already in the queue.*/public synchronized boolean setWifiEnabled(boolean enable) {enforceChangePermission();String packageName = getPackageName(mContext, Binder.getCallingPid());Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()+ ", uid=" + Binder.getCallingUid()+ ", packageName=" + packageName);if (DBG) {Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");}if (enable) {reportStartWorkSource();}mWifiStateMachine.setWifiEnabled(enable);// 调用写入函数WifiLogFileUtil.writeData2Sdcard(mContext, enable, packageName);/** Caller might not have WRITE_SECURE_SETTINGS,* only CHANGE_WIFI_STATE is enforced*/long ident = Binder.clearCallingIdentity();try {handleWifiToggled(enable);} finally {Binder.restoreCallingIdentity(ident);}if (enable) {if (!mIsReceiverRegistered) {registerForBroadcasts();mIsReceiverRegistered = true;}} else if (mIsReceiverRegistered) {mContext.unregisterReceiver(mReceiver);mIsReceiverRegistered = false;}return true;}
3、效果
2015-09-17 14:36:03 true com.xtc.register
2015-09-17 16:12:58 false com.xtc.cleanmemtool
2015-09-17 16:25:48 true com.xtc.cleanmemtool
2015-09-17 17:57:38 true com.eebbk.videoclassjunior
2015-09-17 17:57:39 true com.eebbk.videoclassjunior
2015-09-17 17:58:03 true com.eebbk.videoclassjunior
2015-09-17 21:58:23 false com.xtc.cleanmemtool
2015-09-18 08:10:30 true com.xtc.cleanmemtool
2015-09-18 10:08:03 false com.xtc.cleanmemtool
2015-09-18 10:20:59 true com.xtc.cleanmemtool
2015-09-18 12:35:34 false com.xtc.cleanmemtool
2015-09-18 14:31:12 true com.xtc.cleanmemtool
2015-09-18 23:09:12 false android.process.media
2015-09-18 23:09:12 true android.process.media
2015-09-19 13:26:04 false com.xtc.cleanmemtool
2015-09-19 14:09:04 true com.xtc.cleanmemtool
2015-09-19 15:56:21 false com.xtc.cleanmemtool
2015-09-19 16:15:41 true com.xtc.cleanmemtool
2015-09-19 19:05:44 false com.xtc.cleanmemtool
2015-09-21 08:13:12 true com.xtc.cleanmemtool
2015-09-21 09:57:15 false com.xtc.cleanmemtool
2015-09-21 10:10:43 true com.xtc.cleanmemtool
2015-09-21 11:40:08 false com.xtc.cleanmemtool
2015-09-21 11:45:44 true com.xtc.cleanmemtool
2015-09-21 13:03:09 false com.xtc.cleanmemtool
2015-09-21 14:08:45 true com.xtc.cleanmemtool
2015-09-21 18:23:12 false com.xtc.cleanmemtool
2015-09-21 19:10:05 true com.xtc.cleanmemtool
2015-09-21 19:19:35 false com.xtc.systemsettings
2015-09-21 19:23:07 true com.xtc.systemsettings
2015-09-21 19:23:34 false com.xtc.systemsettings
2015-09-21 19:42:04 true com.eebbk.synchinese
2015-09-21 21:56:59 false com.xtc.cleanmemtool
2015-09-22 08:08:46 true com.xtc.cleanmemtool
这篇关于Android往SD卡写数据范例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!