Java byte stream used in Android, java byte stream android
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6611252.html
Introduction: project development sometimes uses the process of uploading files to the server and then retrieving data from the server to display the data locally. Alternatively, you can enter a piece of text to display the text. IO streams are used in this process.
IO streams are classified into two types: Reader/Writer and InputStream/OutputStream)
In byte streams, we often use FileInputStream (Reading byte data from local files) and FileOutputStream (writing byte data to files)
In this case, how do I use FileOutputStream (File file) and FileInputStream (String name)
The following is a simple explanation:
FileOutputStream (File file) creates a File output stream that writes data to the File represented by the specified file object.
FileInputStream (String name) creates a FileInputStream by opening a connection to the actual file, which is specified by the path name in the file system.
FileOutputStream (File file)
① Convert a file into a stream
The framework provided by the system is as follows:
public FileOutputStream(File file) throws FileNotFoundException { this(file, false);}
Implementation steps: create a file path, create a file under the path, and convert the file into a stream
File cacheDir = getApplicationContext().getFilesDir();File file = new File(cacheDir, "test.txt");FileOutputStream fos = null;fos = new FileOutputStream(file);
② Convert the string into byte array and write it into the stream
The framework provided by the system is as follows:
public void write(byte b[]) throws IOException { write(b, 0, b.length);}
Implementation steps: Get the edited text (use Buffer Knife to replace findViewById), convert the text to a string, and write bytes B. length from the specified byte array to the output stream of this file.
@BindView(R.id.editText)EditText editText;String strWrite = editText.getText().toString();fos.write(strWrite.getBytes());
③ Close the stream
Close the output stream of this file and release all system resources related to this stream
fos.close();
We enter 1 on the EditText control, and the log will return the result to us.
03-24 14:02:17. 591 28576-28576 /? E/MainActivity: File is/data/user/0/com. tnnowu. android. iobutterknife/files/test.txt 03-24 14:02:17. 591 28576-28576 /? E/MainActivity: Fos is java. io. FileOutputStream @ 6eaf805
FileInputStream (String name)
① Read byte data from a local file
The framework provided by the system is as follows:
public FileInputStream(String name) throws FileNotFoundException { this(name != null ? new File(name) : null);}
Implementation steps: get the file path and read the byte data in the file
String filePath = getApplicationContext().getFilesDir().toString() + File.separator + "test.txt";FileInputStream fis = null;fis = new FileInputStream(filePath);
② Write the byte stream into the byte array
The framework provided by the system is as follows:
public int read(byte b[]) throws IOException { return read(b, 0, b.length);}
Implementation step: Read data of up to B. length bytes into a byte array from the input stream.
byte[] buffer = new byte[1024];StringBuilder sb = new StringBuilder();while (fis.read(buffer) != -1) { sb.append(new String(buffer));}
③ Close the stream
Close the input stream of this file and release all system resources related to this stream.
fis.close();
Click the button and the log will return the result to us.
03-24 14:04:50. 048 28576-28576 /? E/MainActivity: Buffer is [B @ 27b505a03-24 14:04:50. 048 28576-28576 /? E/MainActivity: strRead is 1
Complete code (Butter Knife intervention ):
Click to view my other article Butter knife Usage Details
Click here to download the DEMO for free
MainActivity:
Package com. tnnowu. android. iobutterknife; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. util. log; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import butterknife. bindView; import butterknife. butterKnife; import butterknife. onClick; public class MainActivity extends AppCompatActivity implements View. onClickListener {private static final String TAG = "MainActivity"; @ BindView (R. id. editText) EditText editText; @ BindView (R. id. btn_write) Button btnWrite; @ BindView (R. id. bth_read) Button bthRead; @ BindView (R. id. btn_clear) Button btnClear; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. bind (this) ;}@ OnClick ({R. id. btn_write, R. id. bth_read, R. id. btn_clear}) public void onClick (View view) {switch (view. getId ()){
// Use case R. id. btn_write: String strWrite = editText. getText (). toString (); File cacheDir = getApplicationContext (). getFilesDir (); File file = new File (cacheDir, "test.txt"); Log. e (TAG, "File is" + file); try {file. createNewFile ();} catch (IOException e) {e. printStackTrace ();} FileOutputStream fos = null; try {fos = new FileOutputStream (file); Log. e (TAG, "Fos is" + fos);} catch (FileNotFoundException e) {e. printStackTrace ();} try {fos. write (strWrite. getBytes ();} catch (IOException e) {e. printStackTrace ();} try {fos. close ();} catch (IOException e) {e. printStackTrace ();} break;
// FileInputStream uses case R. id. bth_read: String strRead = ""; StringBuilder sb = new StringBuilder (); String filePath = getApplicationContext (). getFilesDir (). toString () + File. separator + "test.txt"; FileInputStream FCM = null; try {FD = new FileInputStream (filePath);} catch (FileNotFoundException e) {e. printStackTrace ();} byte [] buffer = new byte [1024]; Log. e (TAG, "Buffer is" + buffer); try {w Hile (FCM. read (buffer )! =-1) {sb. append (new String (buffer);} FCM. close ();} catch (IOException e) {e. printStackTrace ();} strRead = new String (sb); Log. e (TAG, "strRead is" + strRead); Toast. makeText (getApplicationContext (), strRead, Toast. LENGTH_LONG ). show (); editText. setText (strRead); break; case R. id. btn_clear: editText. setText (""); break ;}}}
Layout code:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. tnnowu. android. iobutterknife. mainActivity "> <EditText android: id =" @ + id/editText "android: layout_width =" match_parent "android: layout_height =" 100dp "android: EMS =" 10 "android: inputType = "textPersonName" android: text = "" android: textSize = "40sp"/> <Button android: id = "@ + id/btn_write" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "write"/> <Button android: id = "@ + id/bth_read" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "read"/> <Button android: id = "@ + id/btn_clear" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "clear"/> </LinearLayout>
FileInputStream and FileOutputStream are suitable for Operating Files in any form (because they are in bytes as the wizard). To operate text files, FileInputReader and FileOutputWriter are used for higher efficiency.
Follow my Sina Weibo website to learn more about Android development!
Focus on science and technology critics, learn about science and technology, innovation, education, and maximize human wisdom and imagination!