1. the principle of file reading and writing in Android:
(1). All files are stored in bytes.
(2). The characters that are not files are reserved on the disk, but the characters are encoded into bytes before they are stored to disk.
(3). When reading a file (especially a text file), it is also a byte-by-byte read to form a byte sequence.
2. the difference between the byte stream and the character streams:
(1). A byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly, and the character streams can.
(2). Byte-flow conversion to character streams can be inputstreamreader,outputstreamwriter.
In general, we usually use the word stream when using.
3. file Read and write steps:
(1). First establish the channel.
(2). Then set up the data storage space.
(3). Then start reading and writing data.
(4). Close the stream.
4. input and output streams:
(1). Input stream: The program reads the data source from the input stream. The data source includes the outside (keyboard, file, network ...), which is to read the data source into the communication channel of the program.
(2). Output stream: The program writes data to the output stream. Output data from the program to the outside (monitor, printer, file, network ...) ) Communication channel.
The code is as follows:
public static byte[] Readinputstream (InputStream inputstream) {
//1. Establish Channel objects
Bytearrayoutputstream OutputStream = new Bytearrayoutputstream ();
2. Define storage space
byte[] buffer = new byte[1024];
3. Start reading file
int len =-1;
try {
if (InputStream!= null) {while
(len = inputstream.read (buffer)!=-1) {
//write data in buffer to Outputstrea Outputstream.write in M object
(buffer, 0, Len);
}
4. Close Flow
outputstream.close ();
Inputstream.close ();
} catch (IOException e) {
e.printstacktrace ();
}
return Outputstream.tobytearray ();
}