Android File Operations

Source: Internet
Author: User

Currently, all devices that can be searched by chance involve file operations, such as computers and mobile phones. Android file operations are similar to those on computers. They can be stored in the built-in storage of mobile phones or SD cards. This blog mainly introduces file operations in the built-in storage of mobile phones.

1. Development Process

(1) Interface Design

(2) design the business layer of android

(3) unit test
(4) set the Controller layer of android

2. Development steps

(1) design the software interface


<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/filename"
/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/filename"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/content"

/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/content"
Android: minLines = "3"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button"
Android: id = "@ + id/button"/>
</LinearLayout>

Here we will also show you the R file.

/* AUTO-generated file. do not modify.
*
* This class was automatically generated by
* Aapt tool from the resource data it found. It
* Shoshould not be modified by hand.
*/
 
Package org. lxh. file;
 
Public final class R {
Public static final class attr {
}
Public static final class drawable {
Public static final int icon = 0x7f020000;
}
Public static final class id {
Public static final int button = 0x7f050002;
Public static final int content = 0x7f050001;
Public static final int filename = 0x7f050000;
}
Public static final class layout {
Public static final int main = 0x7f030000;
}
Public static final class string {
Public static final int app_name = 0x7f040001;
Public static final int button = 0x7f040004;
Public static final int content = 0x7f040003;
Public static final int failure = 0x7f040006;
Public static final int filename = 0x7f040002;
Public static final int hello = 0x7f040000;
Public static final int success = 0x7f040005;
}
}

(2) design the Business Layer

Package org. lxh. service;
 
 
Import java. io. ByteArrayOutputStream;
Import java. io. FileInputStream;
 
Import java. io. FileOutputStream;
 
Import android. content. Context;
Import android. util. Log;
 
Public class FileService {
Private Context context;
 
Public FileService (Context context) {// input context through the constructor
This. context = context;
}
// Save the file
Public void saveFile (String filename, String content) throws Exception {// The Exception is handled by the call.
FileOutputStream out = context. openFileOutput (filename, Context. MODE_PRIVATE );
Out. write (content. getBytes ());
Out. close ();
}
Public String readFile (String filename) throws Exception {// The Exception is handled by the call center.
FileInputStream in = context. openFileInput (filename );
Byte B [] = new byte [1024];
Int len = 0;
ByteArrayOutputStream array = new ByteArrayOutputStream ();

While (len = in. read (B ))! =-1) {// start reading files
Array. write (B, 0, len );
}
Byte data [] = array. toByteArray (); // read the data in the memory

In. close (); // each stream must be closed
Array. close ();
Return new String (data); // convert the byte array to a String and return
}
}

Next we will start unit testing. We will not talk about the environment to be added.

Package org. lxh. test;
 
Import org. lxh. service. FileService;
 
Import android. test. AndroidTestCase;
Import android. util. Log;
 
Public class Test extends AndroidTestCase {
Public static final String TAG = "Test ";
 
Public void testSave (){
FileService service = new FileService (this. getContext ());
Try {
Service. saveFile ("01.txt"," hello ");
} Catch (Exception e ){
Log. I (TAG, e. getMessage ());
}
 
}
Public void testRead (){
FileService service = new FileService (this. getContext ());
Try {
Log. I (TAG, service. readFile ("01.txt "));
} Catch (Exception e ){
Log. e (TAG, e. getMessage ());
}
 
}
}

Check the running effect.

 


 

 


 

 


After the unit test is passed, let's take a look at the effect on the simulator. Before that, let's take a look at the following code.


Package org. lxh. file;
 
Import org. lxh. service. FileService;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
 
Public class FileActivity extends Activity {
Private FileService service;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Service = new FileService (this );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
EditText filename = (EditText) findViewById (R. id. filename );
EditText content = (EditText) findViewById (R. id. content );
Try {
Service. saveFile (filename. getText (). toString (), content. getText (). toString ());
Toast. makeText (FileActivity. this, R. string. success, 1). show ();
} Catch (Exception e ){
Toast. makeText (FileActivity. this, R. string. failure, 1). show ();
Log. e ("FileActivity", e. getMessage ());
} Www.2cto.com
}
});

}
}

If it is successfully saved, a toast notification will be sent to the user.

 


The strings. xml code is also pasted below


<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, FileActivity! </String>
<String name = "app_name"> File Reading </string>
<String name = "filename"> name of the input file </string>
<String name = "content"> input file content </string>
<String name = "button"> Save </string>
<String name = "success"> the file is successfully saved. </string>
<String name = "failure"> failed to save the file </string>
</Resources>

If you have any questions, you can leave a message for me.

Author: chenwill3

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.