android private folder file write and read

2010-12-07  来源:本站原创  分类:Mobile  人气:180 

In the Android platform describes how to read the document before it is necessary to understand the Android platform data storage rules. In other operating systems such as Windows, the application can free or based on specific access rights to access or modify files under the name of other applications and other resources, and in the Android platform, an application of all the data are private.

When an application is installed into the system, package it in a folder will be used to store their own data, only the application of this folder have write permissions to the folder that is located in the private system, Android / data / data / <application package name> directory, and other applications can no longer write in this folder. In addition to storing private data folder, the application also has a SD card write permissions.

Using File I / O method can store data directly to the phone, by default, these files can not be other applications access. Java platform Android platform supports file I / O operations, the main use FileInputStream and FileOutputStream classes to implement these two files are stored and read. For the two class objects in two ways.
A: The first way is like the Java platform, the same way as under the implementation created directly by the constructor, if you need to write data to the open end of the file, you can use the constructor FileOutputStream (File file, boolean append) to append is set to true to achieve. But note that the use of in this way FileOutputStream object if the file does not exist or is not writable, it will throw FileNotFoundException exception.

Second: The second FileInputStream and FileOutputStream object access method is to call Context.openFileInput and Context.openFileOutput two ways to create. Apart from these two methods but, Context object also provides several other methods used for file operations, as shown

Context objects and the description of the Chinese method of operation of the API Name Description
openFileInput (String filename) opens the application's private directory specified file to read private data, returns a FileInputStream object

openFileOutput open the application's private directory to write data in proprietary file specified, returns a FileOutputStream object, if the file does not exist to create the file.

fileList () search application private folder, private files, returns an array of all file names String

deleteFile (String fileName) Deletes the specified file name of the file, the successful return true, false failure returns

Open the file using the openFileOutput method to write data, the need to specify the open mode. The default is zero, that is MODE_PRIVATE. Different models corresponding to the following meanings:

openFileOutput method to open the file mode

Constant meaning
MODE_PRIVATE default mode, the file can only be called the method of application access

MODE_APPEND If the file already exists on the end of the file to write to the data, rather than overwrite the original data.

MODE_WORLD_READABLE all applications granted read access to the file.

MODE_WORLD_WRITEABLE to give all applications written to the file permissions.

The following example to illustrate through a small file under the Android platform, I / O operation, the main function is private in the application data folder, create a file and read the data to the screen of the TextView, this example is relatively There is only one simple class.
Look at the effect of running it.

android private folder file write and read

Activity01

package xiaohang.zhimeng;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class Activity01 extends Activity{
        // Constant, as the encoding format
        public static final String ENCODING = "UTF-8";
        // Defines the name of the file
        String fileName = "test.txt";
        // Write and read data information
        String message = " Welcome to our group discussion questions ";
        TextView textView;
         @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                writeFileData(fileName, message);
                String result = readFileData(fileName);
                textView = (TextView)findViewById(R.id.tv);
                textView.setTextColor(Color.GREEN);
                textView.setTextSize(20.0f);
                textView.setText(result);

        }
         // To specify the file to write to the specified data
         public void writeFileData(String filename, String message){
                 try {
                        FileOutputStream fout = openFileOutput(filename, MODE_PRIVATE);// Get FileOutputStream
                        // The string to be written to the byte array is converted to
                        byte[]  bytes = message.getBytes();
                        fout.write(bytes);// The byte array to write to file
                        fout.close();// Close a file output stream
                } catch (Exception e) {
                        e.printStackTrace();
                }
         }

         // Opens the specified file, reading the data , Returns a string object
         public String readFileData(String fileName){
                 String result="";
                 try {
                        FileInputStream fin = openFileInput(fileName);
                        // Gets the length of the file
                        int lenght = fin.available();
                        byte[] buffer = new byte[lenght];
                        fin.read(buffer);
                        // The byte array is converted to the specified format string
                        result = EncodingUtils.getString(buffer, ENCODING);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
         }
}

Source See Annex

相关文章
  • android private folder file write and read 2010-12-07

    In the Android platform describes how to read the document before it is necessary to understand the Android platform data storage rules. In other operating systems such as Windows, the application can free or based on specific access rights to access

  • java to read the generated private key file openssl generated key issues 2010-08-06

    Openssl generated by the private key file is as follows: ----- BEGIN RSA PRIVATE KEY ----- MIICXwIBAAKBgQDzxjNvQ4oUrDtPaPAnWiiixsiZSuNXIFhSwbKoaRWYLQSSCJPI NuwCi2uRfQemOWp9EWUwxeQOwWxD3XUqZksCj7L7AUd8RXsIlB + Nejbw4KmSxbYn 39eGrf/ipWD4lrsUJMuuNi/QcIG

  • Analysis Android system folder structure 2010-04-04

    \ \ System \ \ app The main store which is the regular downloadable application, you can see is the end of the file format to APK. In this folder the default program for the system components, their installation software will not be here, but \ \ dat

  • Android - save the file and read 2010-06-28

    OF NOTE: Because of my time to test this functionality can not find the file name in Chinese (sdk2.2 + Simulator), which for the master if inadvertently view this article, it can be pointing to this issue, I would be grateful . Ha ha. 1. Preparation

  • Android change the file permissions 2010-11-08

    LINUX under the authority of each file has a property, then how to use Android in a java file permissions to change it? Android, there are two ways to change the file permissions 1. With openFileOutput methods: view plaincopy to clipboardprint? 01.Fi

  • Note --- Android android system folder feature of the SD card Detailed 2011-05-24

    Android system for first-time starters, SD to open when you open a view, there are numerous folders that will always make their own loss, and some see the folder name, but also to guess the point role, but most of all I do not know what to do. 1,. An

  • Python recursively modify the folder. File name. File content in the specified string 2010-02-05

    Has recently been doing XOOPS module, to be honest and not previously exposed PHP, did not conduct a similar interface development, the criteria be denied, time and tight, write to all free to think what to write where. The gall to get out. But custo

  • android R.java file missing how to re-build solutions 2010-04-28

    1. Do not restart Eclipse, do not create your own class files R.java 2. Right-click your project (Project) -> Android Tools -> Fix Project Properties Will once again create the R file, data is not lost

  • android of each file 2010-05-12

    main.xml Analysis <? Xml version = "1.0" encoding = "utf-8"?> version number indicates that the current version of the current encoding that encoding <LinearLayout xmlns: Android = " http://schemas.android.com/apk/res/and

  • android mobile phone file suffix Jie Mi 2010-08-02

    Google Android is a new smart phone operating system, due to strict security design, so many system files in a number of documents is very important, Android mobile phone network will you explain in detail the system and the function of the type of f

  • android mobile phone file suffix Jiemi 2010-08-02

    Google Android is a new smart phone operating system, due to strict security design, so many system files in a number of documents is very important, Android mobile phone network will you explain in detail the system and the function of the type of f

  • android Skills: apk file to decompile and signature packaging (dex2jar & jd, apktool, apk-sign) 2010-08-30

    Jd by dex2jar and we can decompile apk of dex, you can view more perfect java source files; by apktool can decompile apk and other resources in the xml file, and then signed by apk-sign can be made into a modified version of the release apk file. 1.d

  • android frameworks folder (modify the experience 1) 2010-11-02

    1 .. how to add a new application and modify the existing application Here http://www.cnblogs.com/zhangweia/archive/2010/08/19/1803445.html, are detailed. Needs are primarily the following: Enter the command sequence, when to modify add or modify a f

  • Android android.text.format file size and date parsing class 2010-11-13

    Many users may be directly to your J2ME project ported to Android platform blunt, in fact, Google provides us with a good file size and time and date parsing class, which is located android.text.format this package, it provides a powerful analytical

  • Android coding xml file parsing gbk.gb2312 2010-12-12

    Parse xml file Android supports three ways, dom, sax, pull, I use a sax more resolution, but found only the resolution sax default utf-8 encoded xml file; Through online search and finally found the solution: 1. Is to first determine the URL resource

  • Android String resource file format in the way 2010-12-24

    Many times we sensibility in compliance with Google Android in the design of a large number of MVC architecture, it can be to write common code, art and independent of the specific logic developers. Android's resource files related to values / string

  • UE (UltraEdit) using ftp manager on behalf of some of the Linux open the folder, file list does not show 2011-01-10

    How to set the UltraEdit configuration of Sun8 the environment. In the UE on the ftp in the folder list automatically refresh function, edit the file directly double click when you can edit the file, but in some linux systems can not be used. In fact

  • Android Download Chinese file name to solve the problem 2011-05-25

    1, tomcat6.x server 2, android emulator download Such as downloading files on the server: Test. MP3 Download path: http://192.168.1.110:8080/mp3/ test. Mp3 The solution: 1. First set up the server code: find the installation directory under Tomcat se

  • UE (UltraEdit) to open the ftp manager on behalf of some of the Linux folder, file list does not show 2011-01-10

    How to set the environment UltraEdit for Sun8 configuration. In the UE on the ftp in the folder list with automatic refresh function of editing the file by double-clicking you can edit the file, but can not be used on some linux systems. In fact, can

  • Directly create a local folder file, SVN submitted, VS did not show the file on the 2011-09-19

    In the src folder, create the file, then right-click on svn commit, this time to refresh vs code, find the file and not displayed. Then in the other code files refer to the file and found no good. This is because the local paper has not been submitte