Android往SD卡写数据范例

2023-10-08 11:58
文章标签 数据 android sd 范例 卡写

本文主要是介绍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卡写数据范例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/165096

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock