File I/O operations in Android

Source: Internet
Author: User

This section consists of two parts:

1. Access the SD card.

2. Access the storage folder on your mobile phone.

3. Read files in assets.

1. Access the SD card:

1. Edit the interface (reslayoutmain. xml ):

[Java]

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: id = "@ + id/Button01"

Android: layout_width = "128dp"

Android: layout_height = "wrap_content"

Android: text = "open">

  

  

Android: id = "@ + id/button1"

Android: layout_width = "125dp"

Android: layout_height = "wrap_content"

Android: text = "test button"/>

  

Android: id = "@ + id/ScrollView01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

Android: editable = "false"

Android: id = "@ + id/EditText01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

  

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: id = "@ + id/Button01"

Android: layout_width = "128dp"

Android: layout_height = "wrap_content"

Android: text = "open">

  

  

Android: id = "@ + id/button1"

Android: layout_width = "125dp"

Android: layout_height = "wrap_content"

Android: text = "test button"/>

  

Android: id = "@ + id/ScrollView01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

Android: editable = "false"

Android: id = "@ + id/EditText01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

  

2. code editing (srcwyfzclMyActivity. java ):

[Java]

Package wyf. zcl;

Import java. io. File; // introduce related packages

Import java. io. FileInputStream; // introduce related packages

Import android. app. Activity; // introduce related packages

Import android. OS. Bundle; // introduce related packages

Import android. view. View; // introduce related packages

Import android. widget. Button; // introduce related packages

Import android. widget. EditText; // introduce related packages

Import android. widget. Toast; // introduce related packages

Public class MyActivity extends Activity {

/** Called when the activity is first created .*/

Button but; // OPEN Button reference

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

But = (Button) findViewById (R. id. Button01 );

// OPEN button Initialization

But. setOnClickListener (new View. OnClickListener (){

// Add a listener for the open button

@ Override

Public void onClick (View v ){

String contentResult = loadContentFromSDCard ("song. txt ");

// Call the file reading method to obtain the File Content

EditText etContent = (EditText) findViewById (R. id. EditText01 );

// Instantiate EditText

EtContent. setText (contentResult );

// Set EditText content

}

});

}

Public String loadContentFromSDCard (String fileName ){

// Read content from SD card

String content = null; // The content String of the SD card

Try {

File f = new File ("/sdcard/ebook/" + fileName); // File to be read

Int length = (int) f. length ();

Byte [] buff = new byte [length];

FileInputStream FCM = new FileInputStream (f );

FS. read (buff); // read byte. length bytes from the input stream into a byte array.

FCM. close (); // close the input stream and release all system resources associated with the stream

Content = new String (buff, "UTF-8 ");

} Catch (Exception e ){

Toast. makeText (this, "Sorry, no file found ",

Toast. LENGTH_SHORT). show ();

}

Return content;

}

}

Package wyf. zcl;

Import java. io. File; // introduce related packages

Import java. io. FileInputStream; // introduce related packages

Import android. app. Activity; // introduce related packages

Import android. OS. Bundle; // introduce related packages

Import android. view. View; // introduce related packages

Import android. widget. Button; // introduce related packages

Import android. widget. EditText; // introduce related packages

Import android. widget. Toast; // introduce related packages

Public class MyActivity extends Activity {

/** Called when the activity is first created .*/

Button but; // OPEN Button reference

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

But = (Button) findViewById (R. id. Button01 );

// OPEN button Initialization

But. setOnClickListener (new View. OnClickListener (){

// Add a listener for the open button

@ Override

Public void onClick (View v ){

String contentResult = loadContentFromSDCard ("song. txt ");

// Call the file reading method to obtain the File Content

EditText etContent = (EditText) findViewById (R. id. EditText01 );

// Instantiate EditText

EtContent. setText (contentResult );

// Set EditText content

}

});

}

Public String loadContentFromSDCard (String fileName ){

// Read content from SD card

String content = null; // The content String of the SD card

Try {

File f = new File ("/sdcard/ebook/" + fileName); // File to be read

Int length = (int) f. length ();

Byte [] buff = new byte [length];

FileInputStream FCM = new FileInputStream (f );

FS. read (buff); // read byte. length bytes from the input stream into a byte array.

FCM. close (); // close the input stream and release all system resources associated with the stream

Content = new String (buff, "UTF-8 ");

} Catch (Exception e ){

Toast. makeText (this, "Sorry, no file found ",

Toast. LENGTH_SHORT). show ();

}

Return content;

}

}

The running effect is as follows:

2. Access the storage folder on your mobile phone:

Accessing folders on your mobile phone is the same as accessing the SD card. You only need to specify the specific location, but the permission must be upgraded.

3. Read files in assets:

1. In the "assets" directory of the project, create a UTF8-encoded text file "test.txt" as the test object.

2. Edit the interface (reslayoutmain. xml ):

[Java]

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: text = "open"

Android: id = "@ + id/Button01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

Android: id = "@ + id/ScrollView01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

Android: editable = "false"

Android: id = "@ + id/EditText01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

  

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: text = "open"

Android: id = "@ + id/Button01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

Android: id = "@ + id/ScrollView01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

Android: editable = "false"

Android: id = "@ + id/EditText01"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

  

  

  

3. code editing (srcwyfzclMyActivity. java ):

[Java]

Package wyf. zcl;

Import java. io. ByteArrayOutputStream; // introduce related packages

Import java. io. InputStream; // introduce related packages

Import android. app. Activity; // introduce related packages

Import android. OS. Bundle; // introduce related packages

Import android. view. View; // introduce related packages

Import android. widget. Button; // introduce related packages

Import android. widget. EditText; // introduce related packages

Import android. widget. Toast; // introduce related packages

Public class MyActivity extends Activity {

/** Called when the activity is first created .*/

Private Button but; // The Open Button.

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

But = (Button) findViewById (R. id. Button01); // open the Button for instantiation

But. setOnClickListener (new View. OnClickListener () {// open the button to add a listener

@ Override

Public void onClick (View v ){

String contentResult = loadFromAssert ("test.txt ");

EditText etContent = (EditText) findViewById (R. id. EditText01 );

EtContent. setText (contentResult );

}

});

}

Public String loadFromAssert (String fileName ){

String content = null; // result String

Try {

InputStream is = this. getResources (). getAssets (). open (fileName); // open the file

Int ch = 0;

ByteArrayOutputStream baos = new ByteArrayOutputStream (); // implements an output stream

While (ch = is. read ())! =-1 ){

Baos. write (ch); // write the specified byte to the output stream of this byte array.

}

Byte [] buff = baos. toByteArray (); // return the current content of the output stream in the form of a byte array

Baos. close (); // close the stream

Is. close (); // close the stream

Content = new String (buff, "UTF-8"); // sets String Encoding

} Catch (Exception e ){

Toast. makeText (this, "Sorry, the specified file is not found! ", Toast. LENGTH_SHORT). show ();

}

Return content;

}

}

Package wyf. zcl;

Import java. io. ByteArrayOutputStream; // introduce related packages

Import java. io. InputStream; // introduce related packages

Import android. app. Activity; // introduce related packages

Import android. OS. Bundle; // introduce related packages

Import android. view. View; // introduce related packages

Import android. widget. Button; // introduce related packages

Import android. widget. EditText; // introduce related packages

Import android. widget. Toast; // introduce related packages

Public class MyActivity extends Activity {

/** Called when the activity is first created .*/

Private Button but; // The Open Button.

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

But = (Button) findViewById (R. id. Button01); // open the Button for instantiation

But. setOnClickListener (new View. OnClickListener () {// open the button to add a listener

@ Override

Public void onClick (View v ){

String contentResult = loadFromAssert ("test.txt ");

EditText etContent = (EditText) findViewById (R. id. EditText01 );

EtContent. setText (contentResult );

}

});

}

Public String loadFromAssert (String fileName ){

String content = null; // result String

Try {

InputStream is = this. getResources (). getAssets (). open (fileName); // open the file

Int ch = 0;

ByteArrayOutputStream baos = new ByteArrayOutputStream (); // implements an output stream

While (ch = is. read ())! =-1 ){

Baos. write (ch); // write the specified byte to the output stream of this byte array.

}

Byte [] buff = baos. toByteArray (); // return the current content of the output stream in the form of a byte array

Baos. close (); // close the stream

Is. close (); // close the stream

Content = new String (buff, "UTF-8"); // sets String Encoding

} Catch (Exception e ){

Toast. makeText (this, "Sorry, the specified file is not found! ", Toast. LENGTH_SHORT). show ();

}

Return content;

}

}

4. Running effect:

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.