Android: Saving and reading files; android: Saving and reading files

Source: Internet
Author: User

Android: Saving and reading files; android: Saving and reading files


Context. MODE_PRIVATE: The default operation 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 input: openFileOutput ("itcast.txt", Context. MODE_WORLD_READABLE + context.mode_world_writeable+androidhas a set of security models. When the application (.apk) is installed, the system will assign a userid to it. When the application wants to access other resources, such as files, it needs a userid match. 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. L



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, please refer to 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: the "cn. itcast. action is the package where the application is located. When writing code, you should 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: getCacheDir () methods for obtaining/data/<package name>/cache directory getFilesDir () method used to obtain the/data/<package name>/files directory l

-----------------------------------------------------------

Public ClassFileService

{

PrivateContext context;

 

PublicFileService (Context context)

{

This. Context = context;

}

 

/**

* The first parameter of the openFileOutput () method is used to specify the file name. It cannot contain the path separator "/". If the file does not exist, Android

* It is automatically created. 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

* The file is displayed in the/data/<package name>/files directory in the Explorer view.

*/

Public VoidSave (String fileName, String content)ThrowsException

{

/* 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

*/

FileOutputStream stream = context. openFileOutput (fileName, Context.MODE_PRIVATE);

Stream. write (content. getBytes ());

Stream. close ();

}

 

/**

* Reading file content

*@ ParamFileName file name

*@ Return

*@ ThrowsException

*/

PublicString readFile (String fileName)ThrowsException

{

String fileContent = "";

StringBuffer content =NewStringBuffer ();

FileInputStream stream = context. openFileInput (fileName );

Byte[] Buffer =New Byte[1024];

IntLen = 0;

While(Len = stream. read (buffer ))! =-1)

{

FileContent =NewString (buffer, 0, len );

Content. append (fileContent );

}

System.Out. Println (content. toString ());

ReturnContent. toString ();

}

}

 

========================================================== ========================================



















How does android read the storage path of an existing file?

You have written a path in xml and read it through it. You can also obtain the absolute path. You can obtain the path in java.

New users help to save and read files in Android programming!

Write the saved file in the listener of the single-choice group, write the read file in the Button listener, and pass a parameter to get the buttons selected.
 

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.