public class SpActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSp).setOnClickListener(this);
findViewById(R.id.btnSpRead).setOnClickListener(this);
findViewById(R.id.btnFileWrite).setOnClickListener(this);
findViewById(R.id.btnFileReader).setOnClickListener(this);
findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
findViewById(R.id.btnRawRead).setOnClickListener(this);
findViewById(R.id.btnSQlite).setOnClickListener(this);
}
@Override
public class SpActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSp).setOnClickListener(this);
findViewById(R.id.btnSpRead).setOnClickListener(this);
findViewById(R.id.btnFileWrite).setOnClickListener(this);
findViewById(R.id.btnFileReader).setOnClickListener(this);
findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
findViewById(R.id.btnRawRead).setOnClickListener(this);
findViewById(R.id.btnSQlite).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSp:
SharedPreferences sp = this.getSharedPreferences("setting", Context.MODE_PRIVATE);
//SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", "张三");
editor.putInt("age", 24);
editor.putFloat("weight", 110.8f);
//提交
editor.commit();
//editor.apply();
break;
case R.id.btnSpRead:
SharedPreferences spReader = getSharedPreferences("setting", Context.MODE_PRIVATE);
String name= spReader.getString("name", "N");
int age =spReader.getInt("age", 18);
float weight=spReader.getFloat("weight", 80f);
Toast.makeText(this, name+"--"+age+"--"+weight, Toast.LENGTH_LONG).show();
break;
case R.id.btnFileWrite:
writeFiles();
break;
case R.id.btnFileReader:
readFile();
break;
case R.id.btnFileWriteToSd:
writeFilesToSDCard();
break;
case R.id.btnRawRead:
readRawFile();
break;
case R.id.btnSQlite:
Intent in = new Intent(this, DBOptActivity.class);
startActivity(in);
break;
default:
break;
}
}
public void readRawFile(){
Resources res = getResources();
InputStream is= res.openRawResource(R.raw.a);
byte[] buffer = new byte[1024];
int len=0;
StringBuilder sb = new StringBuilder();
try {
while((len= is.read(buffer))!=-1){
String tmp = new String(buffer, 0, len);
sb.append(tmp);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
/**
* 写文件
*/
private void writeFiles(){
FileOutputStream os =null;
try {
os =this.openFileOutput("jerei.txt",Context.MODE_APPEND);
os.write("姓名:张三".getBytes());
os.write("年龄:25".getBytes());
os.write("年龄:25".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(os !=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
os=null;
}
}
}
public void readFile(){
FileInputStream is=null;
StringBuilder sb = new StringBuilder();
try {
is = this.openFileInput("jerei.txt");
byte[] buffer = new byte[1024];
int len=0;
while((len= is.read(buffer))!=-1){
String tmp = new String(buffer,0,len);
sb.append(tmp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
public void writeFilesToSDCard(){
//String filePath = "/mnt/sdcard/jerei";
String filePath=null;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SDCard根路径
filePath=Environment.getExternalStorageDirectory().toString();
filePath=filePath+ File.separator+"jerei"+File.separator+"edu";
File fileParent = new File(filePath);
if(!fileParent.exists()){
fileParent.mkdirs();
}
FileOutputStream os = null;
try {
os = new FileOutputStream(new File(fileParent, "a.txt"));
os.write("向SDCard中写入文件!!".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}