[android]代码库
1.SharedPreferences,存储方式(key,value)
存储:
editor = getSharedPreferences("lock", MODE_WORLD_WRITEABLE).edit(); //MODE设定权限,只能这个程序访问,或者所有程序都可以访问
editor.putString("1","key"); //(key,value)键值对
editor.commit();
取数据:
getSharedPreferences("lock", MODE_WORLD_READABLE).getString("1","")
可以通过Toast显示到屏幕上:
Toast.makeText(MainActivity.this, getSharedPreferences("lock", MODE_WORLD_READABLE).getString("1",""), Toast.LENGTH_SHORT).show();
2.文件读取和保存
MainActivity.Java:
package com.example.savedatademo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button saveBtn = null;
private Button readBtn = null;
private EditText input = null;
private final static String FILE_NAME = "xth.txt"; // 设置文件的名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
saveBtn = (Button) findViewById(R.id.btn1);
readBtn = (Button) findViewById(R.id.btn2);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String data = input.getText().toString();
save(data);
Toast.makeText(MainActivity.this, "保存数据成功!", Toast.LENGTH_SHORT)
.show();
}
});
readBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = read();
Toast.makeText(MainActivity.this, "读取数据为:" + sb,
Toast.LENGTH_SHORT).show();
}
});
}
private StringBuffer read() {
FileInputStream in = null;
Scanner s = null;
StringBuffer sb = new StringBuffer();
try {
in = super.openFileInput(FILE_NAME);
s = new Scanner(in);
while (s.hasNext()) {
sb.append(s.next());
}
} catch (Exception e) {
e.printStackTrace();
}
return sb;
}
private void save(String data) {
FileOutputStream out = null;
PrintStream ps = null;
try {
out = super.openFileOutput(FILE_NAME, Activity.MODE_APPEND);
ps = new PrintStream(out);
ps.println(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.savedatademo.MainActivity" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入" />
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input"
android:text="保存数据" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:text="读取数据"/>
</RelativeLayout>
3.读写SD卡上的数据
3.1申请权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
3.2读写文件的函数
// 文件写操作函数
private void write(String content) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 如果sdcard存在
File file = new File(Environment.getExternalStorageDirectory()
.toString()
+ File.separator
+ DIR
+ File.separator
+ FILENAME); // 定义File类对象
if (!file.getParentFile().exists()) { // 父文件夹不存在
file.getParentFile().mkdirs(); // 创建文件夹
}
PrintStream out = null; // 打印流对象用于输出
try {
out = new PrintStream(new FileOutputStream(file, true)); // 追加文件
out.println(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close(); // 关闭打印流
}
}
} else { // SDCard不存在,使用Toast提示用户
Toast.makeText(this, "保存失败,SD卡不存在!", Toast.LENGTH_LONG).show();
}
}
// 文件读操作函数
private String read() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 如果sdcard存在
File file = new File(Environment.getExternalStorageDirectory()
.toString()
+ File.separator
+ DIR
+ File.separator
+ FILENAME); // 定义File类对象
if (!file.getParentFile().exists()) { // 父文件夹不存在
file.getParentFile().mkdirs(); // 创建文件夹
}
Scanner scan = null; // 扫描输入
StringBuilder sb = new StringBuilder();
try {
scan = new Scanner(new FileInputStream(file)); // 实例化Scanner
while (scan.hasNext()) { // 循环读取
sb.append(scan.next() + "\n"); // 设置文本
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scan != null) {
scan.close(); // 关闭打印流
}
}
} else { // SDCard不存在,使用Toast提示用户
Toast.makeText(this, "读取失败,SD卡不存在!", Toast.LENGTH_LONG).show();
}
return null;
}
by: 发表于:2017-09-29 14:56:10 顶(0) | 踩(0) 回复
??
回复评论