Android file storage

Source: Internet
Author: User
Tags readfile

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

 Packagenet.bwie.localdata.activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportNET.BWIE.LOCALDATA.R;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader; Public classFileactivityextendsAppcompatactivityImplementsView.onclicklistener {protectedButton mreadfilebtn; protectedButton mwritefilebtn; protectedTextView Mresulttv;  Public Static voidstartactivity (Context context) {context.startactivity (NewIntent (context, fileactivity.class)); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Super. Setcontentview (R.layout.activity_file);    Initview (); } @Override Public voidOnClick (view view) {if(View.getid () = =r.id.read_file_btn) {String result=ReadFile ();        Mresulttv.settext (result); } Else if(View.getid () = =r.id.write_file_btn)        {WriteFile (); }    }    //Read File    Privatestring ReadFile () {string FilePath= Environment.getexternalstoragedirectory (). GetPath () + "/abc/"; String FileName= "Xyz.txt"; File File=NewFile (FilePath, fileName); BufferedReader BR=NULL; Try{br=NewBufferedReader (NewInputStreamReader (Newfileinputstream (file)); String result= ""; String Line= "";  while(line = Br.readline ())! =NULL) {result+=Line ; }            returnresult; } Catch(Exception e) {e.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }        return NULL; }    //Write File    Private voidWriteFile () {//External Storage Private path: Android folder//String privatePath = Getexternalfilesdir (null). GetPath ();//private paths are not classified as null//String FilePath = PrivatePath + "/abc/"; //External Storage Common path: Dicm,download,music and other system-provided folders//String publicpath = Environment//. Getexternalstoragepublicdirectory (environment.directory_downloads)//. GetPath ();//String FilePath = Publicpath + "/abc/"; //Custom file pathString RootPath = Environment.getexternalstoragedirectory (). GetPath ();//External storage path (root directory)String FilePath = RootPath + "/abc/"; String FileName= "Xyz.txt"; File File=NewFile (FilePath, fileName); FileOutputStream Fos=NULL; Try{fos=Newfileoutputstream (file); Fos.write ("Asdasdas". GetBytes ()); Toast.maketext ( This, "Success", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); LOG.D ("1507", "Error:" +e.getmessage ()); } finally {            if(Fos! =NULL) {                Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }    Private voidInitview () {mreadfilebtn=(Button) Findviewbyid (R.ID.READ_FILE_BTN); Mreadfilebtn.setonclicklistener (fileactivity. This); MWRITEFILEBTN=(Button) Findviewbyid (R.ID.WRITE_FILE_BTN); Mwritefilebtn.setonclicklistener (fileactivity. This); Mresulttv=(TextView) Findviewbyid (R.ID.RESULT_TV); }}

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi d:orientation= "vertical" tools:context= "net.bwie.localdata.activity.FileActivity" > <button android:id= " @+id/read_file_btn "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android: text= "Read file"/> <button android:id= "@+id/write_file_btn" android:layout_width= "Wrap_content" a        ndroid:layout_height= "Wrap_content" android:text= "Write file"/> <textview android:id= "@+id/result_tv" android:text= "Result" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/></Line Arlayout> 

  

Android file storage

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.