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 Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA