Android reads and writes files

Source: Internet
Author: User

First, we will introduce how to store data using files. Activity provides the openfileoutput () method that can be used to output data to files, the specific implementation process is the same as saving data to a file in the j2se environment.
Public class fileactivity extends activity {
@ Override public void oncreate (bundle savedinstancestate ){
...
Fileoutputstream outstream = This. openfileoutput ("itcast.txt", context. mode_private );
Outstream. Write ("Chuanzhi podcast". getbytes ());
Outstream. Close ();
}
}
The first parameter of the openfileoutput () method is used to specify the file name, Cannot contain 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 View"-"other" in the eclipse menu, expand the android folder in the dialog box, and select the file explorer view below, expand the/data/<package name>/Files directory in the file explorer view 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 ("itcast.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), and cannot be accessed by other programs. Unless context. mode_world_readable or context. mode_world_writeable is specified during creation, only other programs can access it correctly.

To open a file that is private to the application in the/data/<package name>/Files directory, you can use the openfileinput () method provided by activity.
Fileinputstream instream = This. getcontext (). openfileinput ("itcast.txt ");
Log. I ("filetest", readinstream (instream ));
For the readinstream () method, see the remarks below this page.

Or directly use the absolute path of the file:
File file = new file ("/data/CN. itcast. Action/files/itcast.txt ");
Fileinputstream instream = new fileinputstream (File );
Log. I ("filetest", readinstream (instream ));
Note: "cn. itcast. Action" in the above file path is the package of the application. When you are writingCodeReplace the package used by your application.
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 the/data/<package name>/cache directory.
The getfilesdir () method is used to obtain the/data/<package name>/Files directory.

 

From http://www.cnblogs.com/dynasty/archive/2011/03/01/1968430.html

First, we will introduce how to store data using files. Activity provides the openfileoutput () method that can be used to output data to files, the specific implementation process is the same as saving data to a file in the j2se environment.
Public class fileactivity extends activity {
@ Override public void oncreate (bundle savedinstancestate ){
...
Fileoutputstream outstream = This. openfileoutput ("itcast.txt", context. mode_private );
Outstream. Write ("Chuanzhi podcast". getbytes ());
Outstream. Close ();
}
}
The first parameter of the openfileoutput () method is used to specify the file name, Cannot contain 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 View"-"other" in the eclipse menu, expand the android folder in the dialog box, and select the file explorer view below, expand the/data/<package name>/Files directory in the file explorer view 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 ("itcast.txt", context. mode_world_readable + context. mode_world_writeable );

Androidhas a set of security models. When the application program (.apk) is installed, the system will assign a userid to it. 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), and cannot be accessed by other programs. Unless context. mode_world_readable or context. mode_world_writeable is specified during creation, only other programs can access it correctly.

To open a file that is private to the application in the/data/<package name>/Files directory, you can use the openfileinput () method provided by activity.
Fileinputstream instream = This. getcontext (). openfileinput ("itcast.txt ");
Log. I ("filetest", readinstream (instream ));
For the readinstream () method, see the remarks below this page.

Or directly use the absolute path of the file:
File file = new file ("/data/CN. itcast. Action/files/itcast.txt ");
Fileinputstream instream = new fileinputstream (File );
Log. I ("filetest", readinstream (instream ));
Note: "cn. itcast. Action" in the file path above is the package of the application. When you write code, replace it with the package used by your own application.
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 the/data/<package name>/cache directory.
The getfilesdir () method is used to obtain the/data/<package name>/Files directory.

 

From http://www.cnblogs.com/dynasty/archive/2011/03/01/1968430.html

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.