android中的数据存取-方式二:file(文件)

简介:

在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

数据读取

public static String read(Context context, String file) {
String data = "";
try {
FileInputStream stream = context.openFileInput(file);
StringBuffer sb = new StringBuffer();
int c;
while ((c = stream.read()) != -1) {
sb.append((char) c);
}
stream.close();
data = sb.toString();

} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return data;
}

从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。

数据写入

public static void write(Context context, String file, String msg) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
stream.write(msg.getBytes());
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:

public static Properties load(Context context, String file) {
Properties properties = new Properties();
try {
FileInputStream stream = context.openFileInput(file);
properties.load(stream);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return properties;
}

public static void store(Context context, String file, Properties properties) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
properties.store(stream, "");
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
 
/Chapter09_Data_02/src/com/amaker/test/MainActivity.java

 

 
  1. 代码package com.amaker.test;  
  2.  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5.  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.  
  13. public class MainActivity extends Activity {  
  14.     private static final String FILE_NAME="temp.txt";  
  15.     private Button b1,b2;  
  16.     private EditText et1,et2;  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         b1 = (Button)findViewById(R.id.Button01);  
  22.         b2 = (Button)findViewById(R.id.Button02);  
  23.           
  24.         et1 = (EditText)findViewById(R.id.EditText01);  
  25.         et2 = (EditText)findViewById(R.id.EditText02);  
  26.           
  27.         b1.setOnClickListener(new OnClickListener() {  
  28.             @Override 
  29.             public void onClick(View v) {  
  30.                 write(et1.getText().toString());  
  31.             }  
  32.         });  
  33.           
  34.         b2.setOnClickListener(new OnClickListener() {  
  35.             @Override 
  36.             public void onClick(View v) {  
  37.                 et2.setText(read());  
  38.             }  
  39.         });  
  40.     }  
  41.       
  42.     private String read(){  
  43.         try {  
  44.             FileInputStream fis = openFileInput(FILE_NAME);  
  45.             byte[] buffer = new byte[fis.available()];  
  46.             fis.read(buffer);  
  47.             return new String(buffer);  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.         return null;  
  52.     }  
  53.       
  54.     private void write(String content){  
  55.         try {  
  56.             FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);  
  57.             fos.write(content.getBytes());  
  58.             fos.close();  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.     }  
  63. }  
  64.  
  65.  
  66.    
  67. /Chapter09_Data_02/res/layout/main.xml 

 

 
  1. 代码<?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent">  
  5.       
  6.     <TextView android:layout_width="fill_parent" 
  7.         android:layout_height="wrap_content" android:text="File Test" />  
  8.           
  9.     <EditText android:text="" android:id="@+id/EditText01" 
  10.         android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="100px"></EditText>  
  11.           
  12.     <Button android:id="@+id/Button01" android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" android:text="Write"></Button>  
  14.           
  15.     <EditText android:text="" android:id="@+id/EditText02" 
  16.         android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="100px"></EditText>  
  17.           
  18.     <Button android:id="@+id/Button02" android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" android:text="Read"></Button>  
  20.           
  21. </LinearLayout>  
  22.  
  23.  
  24.  
  25.    
  26. /Chapter09_Data_02/AndroidManifest.xml 
 
  1. 代码<?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.amaker.test" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity" 
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.  
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="3" />  
  17.  
  18. </manifest> 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079365


相关文章
|
10月前
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
973 4
|
2月前
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
149 11
|
4月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
4月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
399 19
|
4月前
|
存储 XML Java
Android 文件数据储存之内部储存 + 外部储存
简介:本文详细介绍了Android内部存储与外部存储的使用方法及核心原理。内部存储位于手机内存中,默认私有,适合存储SharedPreferences、SQLite数据库等重要数据,应用卸载后数据会被清除。外部存储包括公共文件和私有文件,支持SD卡或内部不可移除存储,需申请权限访问。文章通过代码示例展示了如何保存、读取、追加、删除文件以及将图片保存到系统相册的操作,帮助开发者理解存储机制并实现相关功能。
1081 2
|
7月前
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
677 83
|
7月前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
420 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
11月前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
Java Android开发 C++
Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手
本文提供了一个Android Studio中JNI使用的模板,包括创建C/C++源文件、编辑CMakeLists.txt、编写JNI接口代码、配置build.gradle以及编译生成.so库的详细步骤,以帮助开发者快速上手Android平台的JNI开发和编译过程。
908 1
|
开发工具 Android开发 开发者
Android平台如何不推RTMP|不发布RTSP流|不实时录像|不回传GB28181数据时实时快照?
本文介绍了一种在Android平台上实现实时截图快照的方法,尤其适用于无需依赖系统接口的情况,如在RTMP推送、RTSP服务或GB28181设备接入等场景下进行截图。通过底层模块(libSmartPublisher.so)实现了截图功能,封装了`SnapShotImpl.java`类来管理截图流程。此外,提供了关键代码片段展示初始化SDK实例、执行截图、以及在Activity销毁时释放资源的过程。此方案还考虑到了快照数据的灵活处理需求,符合GB/T28181-2022的技术规范。对于寻求更灵活快照机制的开发者来说,这是一个值得参考的设计思路。
239 1