Android journey -- start and android journey --

Source: Internet
Author: User

Android journey -- start and android journey --

1. File Reading

Io stream reads files and displays

Package com. helloword; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStreamReader; public class ReadFile {private String text = null; private StringBuffer strbuf = null; public void ReadFile (File file File) {// TODO Auto-generated constructor stub // gets the file output stream FileInputStream (FS); try {FD = new FileInputStream (file ); // convert byte stream to bytes stream BufferedReader br = new BufferedReader (new InputStreamReader (FCM); try {text = br. readLine ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} strbuf. append (text); System. out. println (text );}}

The procedure for accessing "data/com. helloword/file" of android is as follows:

  Open file e to view the Android file.

Open cmd,Go to sdk platform-tool

> Adb shell
$ Su
# Chmod 777/data

  # Chmod 777/data

public class MainActivity extends Activity {public Button bt =null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt = (Button) findViewById(R.id.btcon);bt.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubFile file = new File("data/data/com.helloword/file");ReadFile readfile = new ReadFile();}});}

  


2. read/write operations on SD card files

(1) Register in manifest. xml to obtain the read and write permissions of the SD card.
<! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>

(2) When using SDcard for read/write, several static methods under the Environment class will be used:

1: getDataDirectory (): Get the data directory in Android (data folder in SD card)
2: getDownloadCacheDirectory () Get the downloaded cache directory (the download folder in the SD card)
3: getExternalStorageDirectory () the directory for obtaining external storage generally refers to SDcard (/storage/sdcard0)
4: getExternalStorageState () obtains the current status of the external settings, which generally refers to SDcard. MEDIA_MOUNTED (SDcard exists and can be read and written) has other statuses, you can search in the document.

/*** Determine whether the SDCard exists. [when no external SD card is available, the built-in ROM is also recognized as an SD card] ** @ return */public static boolean isSdCardExist () {return Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED );}

  

* Use the api to obtain the real path of the SD card. Some mobile phone brands will change the path of the SD card.

Environment. getExternalStorageDirectory ();

Read SD card content

  

// Read the SD card content // use FileInputStream to read the File public String ReadFlieInputString (String FileName) throws IOException {String result = null; file File = new File (FileName ); try {FileInputStream isfile = new FileInputStream (file); byte [] B = new byte [isfile. available ()]; isfile. read (B); result = new String (B); System. out. print ("read successful:" + result);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printSta CkTrace ();} return result;} // use BufferRead to read the public String FileBufferRead (String FileName) throws IOException {String result = null; try {BufferedReader bReader = new BufferedReader (new FileReader (FileName); String oneline = ""; StringBuffer sb = new StringBuffer (); while (oneline = bReader. readLine ())! = Null) {sb. append (oneline);} result = sb. toString (); bReader. close (); System. out. println ("read succeeded");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return result ;}

Write files to the SD card

// Write to the SD card // use FileOutputStream to write to the File public Boolean writeSDFile (String FileName, String content) {boolean result = false; try {file File = new File (Environment. getExternalStorageDirectory (), FileName); // obtain the output stream FileOutputStream fos = new FileOutputStream (file); fos. write (content. getBytes (); fos. close (); System. out. println ("written successfully:"); result = true;} catch (Exception e) {e. printStackTrace ();} return result;} // use buffread to write the SD card public Boolean BufferWriteFile (String FileName, String content) {boolean result = false; try {File file = new File (Environment. getExternalStorageDirectory (), FileName); // The second parameter indicates whether to append the content in BufferedWriter bw = new BufferedWriter (new FileWriter (file, true); bw. write (content); bw. flush (); System. out. println ("successfully written"); result = true;} catch (Exception e) {e. printStackTrace ();} return result ;}

The above content is purely self-developed! Mostly refer to http://blog.csdn.net/mad1989/article/details/37568667

 

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.