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

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片