XML:
<LinearLayout xmlns:android=""
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><Button
android:id="@+id/btnWrite" android:layout_width="match_parent" android:layout_marginTop="20dp" android:layout_height="wrap_content" android:text="sp存储" /><Button
android:id="@+id/btnRead" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="sp读取" /><Button
android:id="@+id/btnfileWrite" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文件存储" /> <Button android:id="@+id/btnfileRead" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文件读取" /> <Button android:id="@+id/btnFileWriteToSd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SDCard存储" /> <Button android:id="@+id/btnFileReadToSd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SDCard读取" /></LinearLayout>
Activity:
public class ShareActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sharepreference); findViewById(R.id.btnWrite).setOnClickListener(this); findViewById(R.id.btnRead).setOnClickListener(this); findViewById(R.id.btnFileWriteToSd).setOnClickListener(this); findViewById(R.id.btnfileWrite).setOnClickListener(this); findViewById(R.id.btnfileRead).setOnClickListener(this); findViewById(R.id.btnFileReadToSd).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnWrite: SharedPreferences sp = getSharedPreferences("setting", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name", "张三"); editor.putInt("age", 25); editor.putInt("weight", 120); editor.commit(); break; case R.id.btnRead: SharedPreferences spread = getSharedPreferences("setting", Context.MODE_PRIVATE); String name = spread.getString("name", "null"); int age = spread.getInt("age", 18); int weight = spread.getInt("weight", 15); Toast.makeText(this, name + "--" + age + "--" + weight, Toast.LENGTH_LONG).show(); break; case R.id.btnfileWrite: writeFile(); break; case R.id.btnfileRead: readFile(); break; case R.id.btnFileWriteToSd: writeFilesToSDCard(); break; case R.id.btnFileReadToSd: readFilesFromSDCard(); break; }}
// 写文件 public void writeFile() { FileOutputStream os = null; try { os = this.openFileOutput("jerei.txt", Context.MODE_PRIVATE); os.write("姓名:张三".getBytes()); os.write("年龄:25".getBytes()); os.write("体重:100".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 { if (is != null) { try { is.close(); } catch (IOException e) {e.printStackTrace();
} } } Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show(); }//存进SD卡 public void writeFilesToSDCard() { 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 { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }//读取SD卡
public void readFilesFromSDCard(){ FileInputStream is=null; StringBuilder sb=new StringBuilder(); try { is=this.openFileInput("a.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{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show(); }}