- 浏览: 232651 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (70)
- Spring (5)
- J2EE (5)
- Android (21)
- JavaSe (12)
- JmagicK (1)
- Eclipse (3)
- 重构 (1)
- Struts2 (2)
- 项目经验 (2)
- UML (1)
- 设计模式 (3)
- TDD (1)
- 极限编程 (1)
- 敏捷编程 (1)
- Json (1)
- Java web (1)
- ejb (1)
- mongodb (1)
- Mylyn (1)
- git (1)
- oracle (2)
- 操作系统 (1)
- 数据结构与算法 (2)
- C (1)
- 效率参考 (1)
- Java基础 (3)
- 开源框架 (1)
- 阿里云 (2)
- linux (1)
- html5开发框架 (3)
- ios开发 (2)
- webapp (0)
- 我的创业 (1)
- Java (1)
- IDE (1)
- Java虚拟机 (2)
- 区块链 (2)
最新评论
-
BlueSkyXin:
你真的很厉害,懂得不懂得,都跟我们讲清楚了。你这才叫学知识,透 ...
Struts2值栈的理解 -
wiseyl:
第一种方法: 过滤器不太好,建议在service层时 由于se ...
hibernate+spring mvc,解决hibernate对象懒加载,json序列化失败 -
u012256814:
您好,看了您的这个教程受益不小,我就是想问问您在sha1后面加 ...
Google Map API V2 密钥申请 详细图解 -
ahau10:
OGNL表达式,context, valueStack都讲清楚 ...
Struts2值栈的理解 -
yaolan:
Struts2值栈的理解
AndroidManifest.xml配置
strings.xml
layout/main.xml
FileService.java
文件模式
FileServiceTest.java
MainActivity.java
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.file" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.itcast.file" android:label="Tests for My App" /> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> </manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">文件操作</string> <string name="filename">文件名称</string> <string name="filecontent">请输入文件的内容</string> <string name="button">保存</string> <string name="success">文件保存成功</string> <string name="error">文件保存失败</string> <string name="sdcarderror">SDCard不存在,或者写保护了</string> </resources>
layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filename" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filecontent" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/filecontent" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> </LinearLayout>
FileService.java
package cn.itcast.service; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import android.content.Context; import android.os.Environment; public class FileService { private Context context; public FileService(Context context) { this.context = context; } /** * 以私有文件保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveToSDCard(String filename, String content) throws Exception{ File file = new File(Environment.getExternalStorageDirectory(), filename); FileOutputStream outStream = new FileOutputStream(file); outStream.write(content.getBytes()); outStream.close(); } /** * 以私有文件保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void save(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE); outStream.write(content.getBytes()); outStream.close(); } /** * 以追加方式保存内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveAppend(String filename, String content) throws Exception{// ctrl+shift+y / x FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用从该文件中读取内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveReadable(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用往该文件写入内容 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveWriteable(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 保存内容,注:允许其他应用对该文件读和写 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */ public void saveRW(String filename, String content) throws Exception{ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE); outStream.write(content.getBytes()); outStream.close(); } /** * 读取文件内容 * @param filename 文件名称 * @return * @throws Exception */ public String readFile(String filename) throws Exception{ FileInputStream inStream = context.openFileInput(filename); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while( (len = inStream.read(buffer))!= -1){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//得到文件的二进制数据 outStream.close(); inStream.close(); return new String(data); } }
文件模式
FileServiceTest.java
package cn.itcast.file; import cn.itcast.service.FileService; import android.test.AndroidTestCase; import android.util.Log; public class FileServiceTest extends AndroidTestCase { private static final String TAG = "FileServiceTest"; public void testSave() throws Throwable{ FileService service = new FileService(this.getContext()); service.save("itcast.txt", "www.itcast.cn"); } public void testReadFile() throws Throwable{ FileService service = new FileService(this.getContext()); String result = service.readFile("www.txt"); Log.i(TAG, result); } public void testSaveAppend() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveAppend("append.txt", ",www.csdn.cn"); } public void testSaveReadable() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveReadable("readable.txt", "www.sohu.com"); } public void testSaveWriteable() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveWriteable("writeable.txt", "www.sina.com.cn"); } public void testSaveRW() throws Throwable{ FileService service = new FileService(this.getContext()); service.saveRW("rw.txt", "www.joyo.com"); } }
MainActivity.java
package cn.itcast.file; import cn.itcast.service.FileService; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private FileService fileService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fileService = new FileService(this); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText filenameText = (EditText) findViewById(R.id.filename); EditText contentText = (EditText) findViewById(R.id.filecontent); String filename = filenameText.getText().toString(); String content = contentText.getText().toString(); try { //判断sdcard是否存在于手机上,并且可以进行读写访问 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ fileService.saveToSDCard(filename, content); Toast.makeText(MainActivity.this, R.string.success, 1).show(); }else{ Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show(); } } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, R.string.error, 1).show(); } } }); } }
发表评论
-
Java实现文件加密传输和快速解密读取
2018-06-29 15:29 4200在运营某些项目时,我们可能需要使用到第三方的服务器来存储数据 ... -
Java实现文件加密传输和快速解密读取
2018-06-29 15:37 1在运营某些项目时,我们可能需要使用到第三方的服务器来存储数 ... -
Android4.4来了
2013-11-01 13:59 0Beautiful & Immersive A ... -
Android应用开机自动启动
2013-09-28 19:58 1712背景知识:当Android启动时,会发出一个系统广播,内容为 ... -
Java基础 之软引用、弱引用、虚引用
2013-09-28 19:55 1064在JDK1.2以前的版本中,当一个对象不被任何变 ... -
adb server无法启动方法,结束占用端口的进程
2013-08-05 13:38 8286adb server is out of date. ki ... -
ListViewPager,ListView分页最轻量解决方案
2013-08-05 12:41 0import java.util.concurrent.at ... -
【Android】升级ADT 22 引发的错误
2013-07-05 17:24 993言 今天心血来潮,又把ADT升级了一下,升级到了ADT 2 ... -
Google I/O大会发布Android Studio,安装与不能运行解决
2013-05-16 15:25 20071.下载地址: https://developer.an ... -
Google I/O大会发布Android Studio,安装与不能运行解决等错误解决。
2013-05-16 15:23 1203下载地址: https://developer.andro ... -
使用Git之后出现android library引用失败
2013-04-06 00:04 5421今天在做android项目的使用,因为使用了git做代码托管 ... -
提高Android在eclipse下的编译速度
2013-04-02 15:46 3078大家都知道用eclipse安装adt来进行Android程序 ... -
android no target available 解决
2013-04-02 15:33 10313在安装最新版本的ADT之后,版本21.1.启动eclipse ... -
android学习和广告平台赚钱
2013-01-30 09:29 1615前言: 2011年11月份我开始学习andr ... -
android4.0 使用百度地图不能自动刷新问题
2013-01-21 10:38 2018百度地图,4.0以下的可以正常使用。 但是4.0的就会出 ... -
Google Map API V2 密钥申请 详细图解
2013-01-13 11:03 19423前一阵公司给配了个Mac 谁想项目运行后地图没了,怎么办 ... -
Json转换为Java对象
2012-11-04 19:56 1638package com.chinatech.common ... -
Android Query使用教程
2012-08-05 01:39 7515在android程序设计中,很多是要要实现异步任务,缓存,获取 ... -
设置Activity全屏
2012-08-05 01:27 1284*全屏 在Android下如何实现一个Activit ... -
android 拍照上传照片
2012-08-05 01:23 1317废话不多说,直接进入主题,想要在android中实现拍 ...
相关推荐
android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码
文件操作工具类,包含生成保存,复制,删除,读取,获取文件名,获取文件列表等等,只有你想不到,没有你找不到的Android端工具类,复制到项目中可直接使用
android文件架构详解 cache : 是缓存临时文件夹,据说是除了T-mobile的OTA更新外,别无用处。 红色标记的两个文件是debug模式下产生的 data : 存放用户安装的软件以及各种数据。 default.prop : 默认配置文件 ...
Android文件操作列表显示进入退出删除复制粘贴新建文件等等.rar
Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等
安卓Android源码——安卓Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.rar
安卓Android源码——安卓Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.zip
android文件操作封装的java类,包括根据关键字查找文件,遍历目录,删除目录所有文件等等
安卓开发-Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等
android 文件操作 输入流对象 输出流对象 SDCard读写
Android应用源码之Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等项目安卓应用源码下载.ziAndroid应用源码之Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等项目安卓应用源码...
Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.zip项目安卓应用源码下载Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.zip项目安卓应用源码下载 1.适合学生毕业设计研究...
Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android应用源码之Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.zip
Android高级应用源码-Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等.zip
Android文件操作这是下半部分 是跟上半部分结合的 这是上课做的doc 比较精细全面 有相应代码