Openfileoutput ()

Source: Internet
Author: User

Activity providesOpenfileoutput ()The method can be used to output data to a file. The specific implementation process is the same as saving data to a file in the j2se environment.

Public void save ()
{
Try {
Fileoutputstream
Outstream+this.openfileoutput(+a.txt ", context. mode_world_readable );

Outstream. Write (text. gettext (). tostring (). getbytes ());

Outstream. Close ();

Toast. maketext (myactivity. This, "saved", Toast. length_long). Show ();
}
Catch (filenotfoundexception e ){
Return;
}

Catch (ioexception e ){
Return;
}
}

 

Openfileoutput () The first parameter of the method is used to specify the file name and cannot contain the path separator "/"
If the file does not exist, Android will automatically create it. The created file is saved in the/data/<package name>/Files directory, for example:
/Data/CN. itcast. Action/files/itcast.txt, click "window"-"Show" in the eclipse menu.
View "-" other ", expand the android folder in the dialog window, select the following file explorer view, and then
Expand/data/<package in Explorer View
Name>/Files directory to view the file.
The second parameter of the openfileoutput () method is used to specify the operation mode. There are four modes:
Context. mode_private = 0
Context. mode_append =
32768
Context. mode_world_readable = 1
Context. mode_world_writeable =
2
Context. mode_private: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use context. mode_append.
Context. mode_append: the mode checks whether the file exists and appends the content to the file if it exists. Otherwise, a new file is created.
Context. mode_world_readable and context. mode_world_writeable are used to control whether other applications have the permission to read and write the file.
Mode_world_readable: indicates that the current file can be read by other applications; mode_world_writeable: indicates that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass in:
Openfileoutput(inititcast.txt ",
Context. mode_world_readable + context. mode_world_writeable );

Android has its own security model.Program(.Apk) the system will assign a userid to the application during installation. When the application wants to access other resources, such as files, it needs to match the userid. By default, files created by any application, sharedpreferences, and databases, should be private (in/data/<package
Name>/files), other programs cannot be accessed. Unless context. mode_world_readable or context. mode_world_writeable is specified during creation
Only in this way can other programs access the service correctly.
Read File Content

 

Public void load ()
{
Try {
Fileinputstream
Instream+this.openfileinput(+a.txt ");
Bytearrayoutputstream
Stream = new bytearrayoutputstream ();
Byte [] buffer = new
Byte [2, 1024];
Int length =-1;

While (length = instream. Read (buffer ))! =-1 ){

Stream. Write (buffer, 0, length );
}

Stream. Close ();
Instream. Close ();

Text. settext (stream. tostring ());

Toast. maketext (myactivity. This, "loaded", Toast. length_long). Show ();
}
Catch (filenotfoundexception e ){
E. printstacktrace ();

}
Catch (ioexception e ){
Return;
}
}

 

Only applications that create a private file can access the file. If you want the file to be read and written by other applications, you can specify the context. mode_world_readable and context. mode_world_writeable permissions when creating the file.
Activity also provides the getcachedir () and getfilesdir () methods:
The getcachedir () method is used to obtain/data/<package
Name>/cache directory
The getfilesdir () method is used to obtain/data/<package
Name>/Files directory

  

Put the file into the SD card

 

Use the openfileoutput () method of activity to save files. The files are stored in the mobile phone space. Generally, the mobile phone storage space is not very large, and it is okay to store some small files, it is not feasible to store large files such as videos. For large files like videos, we can store them in sdcard.
What does sdcard do? You can think of it as a mobile hard disk or a USB flash disk.

To use sdcard in the simulator, You need to first create an sdcard (of course, not a real sdcard, but an image file ). You can create sdcard along with the simulator created in eclipse, or use the doscommand to create it, as shown below:

Go to Android in DOS window
In the tools directory of the SDK installation path, enter the following command to create an sdcard with a capacity of 2 GB. The file suffix can be obtained at will. IMG is recommended:

Mksdcard 2048 m d: \ androidtool \ sdcard. img

To access sdcard in a program, you need to apply for the permission to access sdcard.

The permission to access sdcard in androidmanifest. XML is as follows:

<! -Create and delete file permissions in sdcard->

<Uses-Permission
Android: Name = "Android. Permission. mount_unmount_filesystems"/>

<! -Write Data to sdcard->

<Uses-Permission
Android: Name = "Android. Permission. write_external_storage"/>

 

To store files in sdcard, the program must first determine whether the mobile phone is equipped with sdcard and can read and write data.

Note: The permission to access sdcard must be added to androidmanifest. xml.

If (environment. getexternalstoragestate (). Equals (environment. media_mounted )){

File sdcarddir =
Environment. getexternalstoragedirectory (); // get the sdcard directory

File SaveFile = new file (sdcarddir, cmda.txt ");

Fileoutputstream outstream = new
Fileoutputstream (SaveFile );

Outstream. Write ("test". getbytes ());

Outstream. Close ();

}

The environment. getexternalstoragestate () method is used to obtain the sdcard status. If the mobile phone has an sdcard and can be read and written, the returned state is environment. media_mounted.

The environment. getexternalstoragedirectory () method is used to obtain the sdcard directory. To obtain the sdcard directory, you can also write:

File sdcarddir = new file ("/sdcard"); // get the sdcard directory

File SaveFile = new file (sdcarddir, “itcast.txt ");

// The above two sentencesCodeYou can combine the following sentence: file SaveFile = new file ("/sdcard/a.txt ");

Fileoutputstream outstream = new fileoutputstream (SaveFile );

Outstream. Write ("test". getbytes ());

Outstream. Close ();

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.