---------------------- 路漫漫其修远兮,吾将上下而求索。学无止境!----------------------
将文件保存到内存(data/data目录下)
先介绍一下Context,上下文,就是一个类;提供一些方便的api,可以得到应用程序的环境、环境包名、安装路径、文件的路径、资源的路径、资产的路径。
MainActivity.java
package com.lee.login;
import java.util.Map;
import com.lee.service.MyService;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener {
private EditText editTextName, editTextPassword;
private Button login;
private MyService myService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = (EditText) this.findViewById(R.id.etName);
editTextPassword = (EditText) this.findViewById(R.id.etPassword);
login = (Button) this.findViewById(R.id.login);
//button的点击事件
login.setOnClickListener(this);
//得到map
Map<String, String> map = myService.getInfo(this);
if (map != null) {
editTextName.setText(map.get("name"));
editTextPassword.setText(map.get("password"));
}
}
public void login() {
//得到登陆信息,并保存到起来
String name = editTextName.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
Toast.makeText(this, name + " : " + password, Toast.LENGTH_SHORT)
.show();
myService.saveInfo(this, name, password);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
login();
}
}
MyService.java
package com.lee.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
public class MyService {
public static boolean saveInfo(Context context, String name, String password) {
//通过context得到data/data/files文件夹路径
File file = new File(context.getFilesDir(), "login.txt");
try {
//向文件写入注册信息
FileOutputStream fos = new FileOutputStream(file);
String str = name + "##" + password;
fos.write(str.getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
public static Map<String, String> getInfo(Context context) {
File file = new File(context.getFilesDir(), "login.txt");
//由文件读取信息,并写入map
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String text = br.readLine();
Map<String, String> map = new HashMap<String, String>();
String[] strs = text.split("##");
map.put("name", strs[0]);
map.put("password", strs[1]);
return map;
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
}
上面程序中的File file = new File(context.getFilesDir(), "login.txt");可以使用context.openFileOutput("login.txt",mode);替换.
mode:
Context.MODE_PRIVATE 私有
Context.MODE_WORLD_READABLE 只读
Context.MODE_WORLD_WRITEABLE 只写
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE 读写
如果考虑将信息保存到内存卡,那程序代码只需改变文件的路径,这里通过Environment得到内存卡的状态,并且判断正常后存入即可,不过,仍要在AndroidManifest.xml中加入
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
修改后的代码
public static boolean saveInfo(Context context, String name, String password) {
// 通过Environment得到SD的状态和路径
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(),
"login.txt");
try {
// 向文件写入注册信息
FileOutputStream fos = new FileOutputStream(file);
String str = name + "##" + password;
fos.write(str.getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
}
return true;
}
public static Map<String, String> getInfo(Context context) {
// 通过Environment得到SD的状态和路径
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(),
"login.txt");
// 由文件读取信息,并写入map
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String text = br.readLine();
Map<String, String> map = new HashMap<String, String>();
String[] strs = text.split("##");
map.put("name", strs[0]);
map.put("password", strs[1]);
return map;
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
return null;
}
获取手机内部和外部存储空间的大小
步骤:
1.得到所要查询的存储空间的路径
File file = Environment.getExternalStorageDirectory();
2.根据路径,得到StatFs状态
StatFs stat = new StatFs(file.getPath());
3.根据StatFs状态得到各个属性,包括各种存储块与存储块大小
long availableBlocks = stat.getAvailableBlocks();
long blockCount = stat.getBlockCount();
long blockSize = stat.getBlockSize();
long freeBlocks = stat.getFreeBlocks();
4.对查询到的数字,按照字节大小的特点进行格式化。
Formatter formatter = new Formatter();
String total = formatter.formatFileSize(this, blockSize*blockCount);
String free = formatter.formatFileSize(this, blockSize*freeBlocks);
代码:
package com.lee.rom;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.app.Activity;
import android.text.format.Formatter;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) this.findViewById(R.id.tv);
textView.setText(this.getSdInfo()+"\n"+this.getRomInfo());
}
/**
* 得带sd的存储空间信息
* @return
*/
public String getSdInfo() {
//此处为data/data路径下的存储空间
File file = Environment.getExternalStorageDirectory();
//得到路径下的存储空间的状态,根据此状态得到各种属性
StatFs stat = new StatFs(file.getPath());
long availableBlocks = stat.getAvailableBlocks();
long blockCount = stat.getBlockCount();
long blockSize = stat.getBlockSize();
long freeBlocks = stat.getFreeBlocks();
Formatter formatter = new Formatter();
//格式化,将数字转化为存储空间的字节大小
String total = formatter.formatFileSize(this, blockSize*blockCount);
String free = formatter.formatFileSize(this, blockSize*freeBlocks);
return "SD总空间:"+(total)+" 可用空间:"+(free);
}
/**
* 得到Rom的存储信息
* @return
*/
public String getRomInfo() {
//此处为SDCard的存储空间
File file = Environment.getDataDirectory();
StatFs stat = new StatFs(file.getPath());
long availableBlocks = stat.getAvailableBlocks();
long blockCount = stat.getBlockCount();
long blockSize = stat.getBlockSize();
long freeBlocks = stat.getFreeBlocks();
Formatter formatter = new Formatter();
String total = formatter.formatFileSize(this, blockSize*blockCount);
String free = formatter.formatFileSize(this, blockSize*freeBlocks);
return "ROM总空间:"+(total)+" 可用空间:"+(free);
}
}
运行结果:
---------------------- 路漫漫其修远兮,吾将上下而求索。学无止境!----------------------
我的博客:http://blog.csdn.net/helloxiaobi