国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

I/O流的梳理和小結

2019-11-09 16:50:16
字體:
來源:轉載
供稿:網友

版權聲明:本文原創作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl

package cc.testio;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;/** * * IO流小結: * * 1 InputStream和OutputStream中出和入的方向都是相對于內存而言的. * 讀進內存的采用InputStream,寫出內存的采用OutputStream * * 2 在使用IO流時請注意區分采用字節流還是字符流 * 比如對于圖片的讀取就不能采用字符流 * * 3 原來的幾篇和IO相關的博文,現已DePRecated * http://blog.csdn.net/lfdfhl/article/details/8195216 * http://blog.csdn.net/lfdfhl/article/details/8195214 * http://blog.csdn.net/lfdfhl/article/details/8195422 * http://blog.csdn.net/lfdfhl/article/details/8195375 * http://blog.csdn.net/lfdfhl/article/details/12174041 * * * 原創作者 * 谷哥的小弟 * * 博客地址 * http://blog.csdn.net/lfdfhl * */public class MainActivity extends AppCompatActivity { private ImageView mImageView; private DownLoaderAsyncTask mAsyncTask; private String SDCardDir; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { SDCardDir= Environment.getExternalStorageDirectory()+ File.separator; mImageView = (ImageView) findViewById(R.id.imageView); test1(); //test2(); //test3(); //test4(); } /** * * 利用IO流拷貝文本文件 * * 原創作者 * 谷哥的小弟 * * 博客地址 * http://blog.csdn.net/lfdfhl */ private void test1() { // 字節輸入流 FileInputStream fileInputStream = null; // 字符輸入流 InputStreamReader inputStreamReader = null; // 帶緩沖的字符輸入流 BufferedReader bufferedReader = null; // 字節輸出流 FileOutputStream fileOutputStream = null; // 字符輸出流 OutputStreamWriter outputStreamWriter = null; // 帶緩沖的字符輸出流 BufferedWriter bufferedWriter = null; try { fileInputStream = new FileInputStream(SDCardDir + "test.txt"); //原文件的編碼格式為GBK,故在此按該編碼方式讀取字符避免亂碼 inputStreamReader = new InputStreamReader(fileInputStream,"GBK"); bufferedReader = new BufferedReader(inputStreamReader); fileOutputStream = new FileOutputStream(SDCardDir + "eeee.txt"); outputStreamWriter = new OutputStreamWriter(fileOutputStream); bufferedWriter = new BufferedWriter(outputStreamWriter); String line = null; while ((line = bufferedReader.readLine()) != null) { bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } } catch (Exception e) { } finally { if (null != bufferedWriter) { try { bufferedWriter.close(); } catch (IOException e) { } } if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { } } } } /** * * 利用IO流拷貝本地圖片 * * 原創作者 * 谷哥的小弟 * * 博客地址 * http://blog.csdn.net/lfdfhl */ private void test2() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(SDCardDir + "beauty.jpg"); fileOutputStream = new FileOutputStream(SDCardDir + "yyyy.jpg"); int len = 0; byte buffer[] = new byte[1024 * 2]; while ((len = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); fileOutputStream.flush(); } } catch (Exception e) { } finally { try { if (null != fileInputStream) { fileInputStream.close(); } if (null != fileOutputStream) { fileOutputStream.close(); } } catch (Exception e) { } } } /** * * 利用IO流加載本地圖片 * * 在得到FileInputStream之后可以調用系統API獲取Bitmap: * Bitmap bitmap=BitmapFactory.decodeStream(fileInputStream); * 當然,在此我們不采用該方法而是自己用IO流來讀取Bitmap。 * * 嗯哼,繼續說這個例子. * 在此請注意對于buffer的大小指定 * byte [] buffer=new byte[fileInputStream.available()]; * 具體大小為fileInputStream.available(). * 請注意文檔的描述: * public int available() throws IOException * 返回可以不受阻塞地從此文件輸入流中讀取的字節數。 * 在該示例中圖片保存于本地,故可不受阻塞地讀取該文件 * 所以可通過fileInputStream.available()獲取到文件的大小 * * 在為buffer設置完大小后再將數據讀入到buffer中 * fileInputStream.read(buffer); * 再解析得到Bitmap * Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); * * 原創作者 * 谷哥的小弟 * * 博客地址 * http://blog.csdn.net/lfdfhl */ private void test3(){ FileInputStream fileInputStream=null; try { fileInputStream=new FileInputStream(SDCardDir+"beauty.jpg"); byte [] buffer=new byte[fileInputStream.available()]; fileInputStream.read(buffer); Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); mImageView.setImageBitmap(bitmap); }catch (Exception e){ }finally { try { if (null != fileInputStream) { fileInputStream.close(); } } catch (Exception e) { } } } /** * * 利用IO流加載網絡圖片 * * 在該示例中圖片不再在本地而是在遠程。 * 在這種情況下可能發生網絡阻塞,企圖利用 * inputStream.available()獲取圖片的大小是錯誤的; * 此時,利用該方法獲取到的值遠小于圖片大小。 * * 那么該怎么做呢? * 可以這樣: * while ((len = inputStream.read(buffer)) != -1) { * byteArrayOutputStream.write(buffer, 0, len); * } * 1 利用inputStream將數據不斷地讀入到buffer中,即: * while ((len = inputStream.read(buffer)) != -1) * 2 將buffer中的數據寫入到ByteArrayOutputStream中,即: * byteArrayOutputStream.write(buffer, 0, len); * 3 讀取完數據后將ByteArrayOutputStream中的全部字節數據保存到字節數組中 * byte[] imageData = byteArrayOutputStream.toByteArray(); * 4 解析生成Bitmap * Bitmap bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length); * * 此示例中ByteArrayOutputStream充當了"中轉站"的作用。 * 我們不能一次性從inputStream中獲取到所有的數據,所以就通過 * 多次讀取來獲得所有的數據;每次讀取后就將數據暫存于ByteArrayOutputStream. * 當完成多次讀取后再將全部數據從ByteArrayOutputStream中取出 * * 原創作者 * 谷哥的小弟 * * 博客地址 * http://blog.csdn.net/lfdfhl */ private void test4(){ mAsyncTask = new DownLoaderAsyncTask(); mAsyncTask.execute("http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg"); } private class DownLoaderAsyncTask extends AsyncTask<String, Integer, Bitmap> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Bitmap doInBackground(String... params) { InputStream inputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { URL imageUrl = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[ 1024 ]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } byte[] imageData = byteArrayOutputStream.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); return bitmap; } } catch (Exception e) { } finally { try { if (null != inputStream) { inputStream.close(); } if (null != byteArrayOutputStream) { byteArrayOutputStream.close(); } } catch (Exception e) { } } return null; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (null != bitmap) { mImageView.setImageBitmap(bitmap); } } }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长泰县| 曲靖市| 云林县| 南康市| 梅州市| 贵溪市| 昭苏县| 大方县| 武义县| 监利县| 高陵县| 房山区| 龙里县| 元阳县| 柘城县| 长顺县| 香格里拉县| 潢川县| 阿巴嘎旗| 黎川县| 科尔| 乌恰县| 丁青县| 井陉县| 临江市| 栖霞市| 西华县| 灵璧县| 佛山市| 会昌县| 新野县| 许昌市| 怀仁县| 江西省| 铁力市| 灵璧县| 青海省| 海安县| 邮箱| 家居| 嘉祥县|