標籤:blog java os io 檔案 ar 資料 art div
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);}@Overridepublic 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 blocke.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 blocke.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();}}}}}
資料存放區的三種方式