[Android learning] Four storage methods for Android data (2) -- File

Source: Internet
Author: User

File: A File (I/O) storage method, which is commonly used to store a large amount of data. However, updating data is difficult.

The following program demonstrates how to read and write files in the application data folder. The interface of this program is as simple as it contains only two text input boxes and two buttons: the first text box and the button are used to process the input; the second text box is used for data processing. The Code is as follows:

 

package com.ye_yun_lin.filetest;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintStream;import javax.security.auth.PrivateCredentialPermission;import android.os.Bundle;import android.R.integer;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {final String FILE_NAME = "MyData";private EditText editText1;private EditText editText2;private Button button1;private Button button2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText1=(EditText)findViewById(R.id.edittext1);button1=(Button)findViewById(R.id.button1);editText2=(EditText)findViewById(R.id.edittext2);button2=(Button)findViewById(R.id.button2);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {write(editText1.getText().toString());editText1.setText("");}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {editText2.setText(read());}});}private String read(){try {FileInputStream fileInputStream=openFileInput(FILE_NAME);byte[] buff=new byte[1024];int hasRead=0;StringBuilder sBuilder=new StringBuilder("");while ((hasRead=fileInputStream.read(buff))>0) {sBuilder.append(new String(buff,0,hasRead));}fileInputStream.close();return sBuilder.toString();} catch (Exception e) {e.printStackTrace();}return null;}private void write(String content){try {FileOutputStream fileOutputStream=openFileOutput(FILE_NAME, MODE_APPEND);PrintStream printStream=new PrintStream(fileOutputStream);printStream.println(content);printStream.close();} catch (Exception e) {e.printStackTrace();}}}


 

 

Example: SD card file browser:

When the program starts, the system obtains all the files and folders in the/mnt/sdcard directory and displays them using ListView. When you click the specified list of ListView, the system displays all the folders and files in the list.

The program's interface layout file is as follows:

 

     
      
       
   
   
  
 

Main Code:

 

 

Package com. ye_yun_lin.sdcard; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. OS. bundle; import android. r. integer; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. listView; import android. widget. simpleAdapter; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private TextView textView; private ListView; private Button button; private File currentParent; private File [] currentFiles; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. path); listView = (ListView) findViewById (R. id. list); button = (Button) findViewById (R. id. parent); File root = new File ("/mnt/sdcard/"); if (root. exists () {currentParent = root; currentFiles = root. listFiles (); inflateListView (currentFiles);} listView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
 Parent, View view, int position, long id) {if (currentFiles [position]. isFile () return; File [] tmp = currentFiles [position]. listFiles (); if (tmp = null | tmp. length = 0) {Toast. makeText (MainActivity. this, "the current path is not accessible or there are no files under this path", Toast. LENGTH_SHORT);} else {currentParent = currentFiles [position]; currentFiles = tmp; inflateListView (currentFiles) ;}}); button. setOnClickListener (new OnClickListener () {@ Overridepublic void OnClick (View v) {try {if (! CurrentParent. getCanonicalPath (). equals ("/mnt/sdcard") {currentParent = currentParent. getParentFile (); currentFiles = currentParent. listFiles (); inflateListView (currentFiles) ;}} catch (IOException e) {e. printStackTrace () ;}});} private void inflateListView (File [] files) {List
 
  
> ListItemsListView = new ArrayList
  
   
> (); For (int I = 0; I
   
    
ListItemMap = new HashMap
    
     
(); If (files [I]. isDirectory () {listItemMap. put ("icon", R. drawable. folder);} else {listItemMap. put ("icon", R. drawable. file);} listItemMap. put ("fileName", files [I]. getName (); listItemsListView. add (listItemMap);} SimpleAdapter simpleAdapter = new SimpleAdapter (this, listItemsListView, R. layout. line, new String [] {"icon", "fileName"}, new int [] {R. id. icon, R. id. file_name}); listView. setAdapter (simpleAdapter); try {textView. setText ("current path:" + currentParent. getCanonicalPath ();} catch (IOException e) {e. printStackTrace ();}}}
    
   
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.