Android SD card file read/write

Source: Internet
Author: User

 

Use the buttons in the upper-right corner to export and import files from the simulator.

Program running result

After running, delete in the file browser is deleted.

 

 

FileHelper. java is a help class for files. It creates, deletes, and reads files.

 

 

Package com. zeph. android. fileoperate;

 

Import java. io. File;

Import java. io. FileInputStream;

Import java. io. FileNotFoundException;

Import java. io. IOException;

 

Import android. content. Context;

Import android. OS. Environment;

 

Public class FileHelper {

 

Private Context context;

/** Whether the SD card exists **/

Private boolean hasSD = false;

/** SD card path **/

Private String SDPATH;

/** Path of the current package **/

Private String FILESPATH;

 

Public FileHelper (Context context ){

This. context = context;

HasSD = Environment. getExternalStorageState (). equals (

Android. OS. Environment. MEDIA_MOUNTED );

SDPATH = Environment. getExternalStorageDirectory (). getPath ();

FILESPATH = this. context. getFilesDir (). getPath ();

}

 

/**

* Create a file on the SD card

*

* @ Throws IOException

*/

Public File createSDFile (String fileName) throws IOException {

File file = new File (SDPATH + "//" + fileName );

If (! File. exists ()){

File. createNewFile ();

}

Return file;

}

 

/**

* Delete files on the SD card

*

* @ Param fileName

*/

Public boolean deleteSDFile (String fileName ){

File file = new File (SDPATH + "//" + fileName );

If (file = null |! File. exists () | file. isDirectory ())

Return false;

Return file. delete ();

}

 

/**

* Read text files from the SD card

*

* @ Param fileName

* @ Return

*/

Public String readSDFile (String fileName ){

StringBuffer sb = new StringBuffer ();

File file = new File (SDPATH + "//" + fileName );

Try {

FileInputStream FCM = new FileInputStream (file );

Int c;

While (c = FCM. read ())! =-1 ){

Sb. append (char) c );

}

FCM. close ();

} Catch (FileNotFoundException e ){

E. printStackTrace ();

} Catch (IOException e ){

E. printStackTrace ();

}

Return sb. toString ();

}

 

Public String getFILESPATH (){

Return FILESPATH;

}

 

Public String getSDPATH (){

Return SDPATH;

}

 

Public boolean hasSD (){

Return hasSD;

}

}

 

Activity Type

 

 

 

Package com. zeph. android. fileoperate;

 

Import java. io. IOException;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. widget. TextView;

 

Public class FileOperateActivity extends Activity {

 

Private TextView hasSDTextView;

Private TextView SDPathTextView;

Private TextView FILESpathTextView;

Private TextView createFileTextView;

Private TextView readFileTextView;

Private TextView deleteFileTextView;

Private FileHelper helper;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

HasSDTextView = (TextView) findViewById (R. id. hasSDTextView );

SDPathTextView = (TextView) findViewById (R. id. SDPathTextView );

FILESpathTextView = (TextView) findViewById (R. id. FILESpathTextView );

CreateFileTextView = (TextView) findViewById (R. id. createFileTextView );

ReadFileTextView = (TextView) findViewById (R. id. readFileTextView );

DeleteFileTextView = (TextView) findViewById (R. id. deleteFileTextView );

 

Helper = new FileHelper (getApplicationContext ());

 

HasSDTextView. setText ("Whether the SD card exists:" + helper. hasSD ());

SDPathTextView. setText ("SD card path:" + helper. getSDPATH ());

FILESpathTextView. setText ("package path:" + helper. getFILESPATH ());

 

Try {

CreateFileTextView. setText ("Create File :"

+ Helper. createSDFile ("test.txt"). getAbsolutePath ());

} Catch (IOException e ){

E. printStackTrace ();

}

 

DeleteFileTextView. setText ("Whether the file is successfully deleted :"

+ Helper. deleteSDFile ("delete.txt "));

 

ReadFileTextView. setText ("Read File:" + helper. readSDFile ("benzeph.txt "));

}

}

 

Layout. xml

 

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical">

 

<TextView

Android: id = "@ + id/hasSDTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"/>

 

<TextView

Android: id = "@ + id/SDPathTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"/>

 

<TextView

Android: id = "@ + id/FILESpathTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"/>

 

<TextView

Android: id = "@ + id/createFileTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "false"/>

<TextView

Android: id = "@ + id/readFileTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "false"/>

<TextView

Android: id = "@ + id/deleteFileTextView"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "false"/>

 

</LinearLayout>

 

Note that the conditions for reading external devices must be added to the Manifest file. Previously, I forgot to add the file, and the file cannot be created or deleted.

 

 

 

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

 

From Ben Zeph's Code Cloud

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.