Android Internal File read

Source: Internet
Author: User

How to manage Android files
Android uses a Linux-based file system, and access to and management of files is limited by permission settings.
In Linux systems, file permissions describe the restrictions on the operation of the file by the creator, the same group of users, and other users.
X indicates that it is operational, R is readable, W is writable, D represents a folder, and--represents a normal file.
Generate this file permission with the program personnel set
Types of Android storage files
(internal storage) Program developers can create and access to the program's own private files;
(Resource store) to access the original files and XML files saved in the resource folder;
(external storage) to save files on external storage devices such as SD cards

The Android system agrees that the application creates private files that are accessible only to itself, and that the files are on the internal memory of the device, in the/data/data/<package name>/files folder under the Linux system
The Android system not only supports standard Java IO classes and methods, but also provides functions to simplify the process of reading and writing streaming files.
FileOutputStream openfileoutput (String filename int mode)
FileInputStream openfileinput (String filename)
The parameter file does not agree to include a slash that describes the narrative path (its storage location is fixed)
Interview Mode :

Mode_private private mode. Defect mode, the file can only be visited by the file creation program, or a program with the same UID.
Mode_append Append mode, assuming the file already exists. The new data is added at the end of the file.


Mode_world_readable Global read mode, agreeing to read private files regardless of program.
Mode_world_writeable Global write mode. Agree that no matter what program is written to the private file.

Three main methods of reading

abstract int Read (): reads a byte of data. and returns the data that was read. Assuming that 1 is returned, the end of the input stream is read.
int read (byte[] b): reads the data into a byte array, returning the actual number of bytes read at the same time. Assuming that 1 is returned, the end of the input stream is read.
int read (byte[] b, int off, int len): reads the data into an array of bytes. Returns the number of bytes actually read at the same time.

Assuming that 1 is returned, the end of the input stream is read. OFF specifies the starting offset where the data is stored in array B. LEN Specifies the maximum number of bytes to read.
Other methods
Long n: Skips n bytes in the input stream and returns the number of bytes actually skipped.


int available (): Returns the number of bytes that can be read without clogging.


void Close (): closes the input stream and frees the system resources associated with this stream.
void mark (int readlimit): A tag is placed at the current position of the input stream, assuming that the number of bytes read is greater than the value set by the Readlimit, the stream ignores the token.
void Reset (): Returns to the previous tag.
Boolean marksupported (): Tests whether the current stream supports the mark and reset methods. Assuming support, returns TRUE, otherwise false is returned.

Three main methods of writing

abstract void Write (int b): Writes a byte to the output stream.


void Write (byte[] b): Writes all the bytes in array b toward the output stream.
void Write (byte[] b, int off, int len): writes to the output stream a Len byte of data from offset off in array B.
Other methods
void Flush (): Flushes the output stream, forcing the output bytes in the buffer to be written out.
void Close (): Closes the output stream, releasing the system resources associated with this stream.

Operations on files and streams easy throws an exception, so it is necessary to use the Try-catch statement

Main Core Code

The first is to create a new file
Private final String file_name = "Myfile01.txt";
Write a file
FileOutputStream fos = null;//declares a global variable
Note the following statement to throw exception handling FileNotFoundException E. IOException E
FOS = Openfileoutput (file_name,context.mode_private);//write streaming file process

Function. The permissions here are private.
string text = Entrytext.gettext (). toString ();//convert the input into a string
Fos.write (Text.getbytes ());//convert the content converted to a string into bytes, then write
The following statement is written in finally
Fos.flush ();//write the contents of the cache to a file
Fos.close ();//Close stream

Read the file

FileInputStream FIS = null;//defines a global variable FIS = openfileinput (file_name);//Open the file to be read if (fis.available () = = 0) {//infer if the file is empty. Returns the return directly as empty;} byte[] readbytes = new byte[fis.available ()];//Convert the contents of the file into bytes while (Fis.read (readbytes)! =-1) {//Read the file. Until the last}string text = new String (readbytes) is read, and/or the read byte is converted to a string

another way to read a file

String Path = "/data/data/cn.itcast.file/files/writeable.txt";//Gets the file path, "filename"  = new file (path);//Create a Document object  FileInputStream instream = new FileInputStream (file);//Read file  byte[] buffer = new byte[1024];//cache  int len = 0;  Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();  while (len = instream.read (buffer))! =-1) {//Until the end of the file   is read outstream.write (buffer, 0, Len);  }  byte[] data = Outstream.tobytearray ();//Get the binary data of the file  outstream.close ();  Instream.close ();  LOG.I (TAG, new String (data));


Android Internal File read

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.