android數(shù)據(jù)存儲之文件存儲方法
文件存儲是 Android 中最基本的一種數(shù)據(jù)存儲方式,它不對存儲的內(nèi)容進(jìn)行任何的格式化處理,所有數(shù)據(jù)都是原封不動的保存到文件當(dāng)中的。
概述
文件存取的核心就是輸入流和輸出流。
Android文件的操作模式
文件的相關(guān)操作方法
文件讀寫的實(shí)現(xiàn)
openFileOutput和openFileInput方法
/** * openFIleOutput ,openFileInput * 這兩種方法同sp一樣只能講文件保存到手機(jī)內(nèi)存固定的路徑中, * 默認(rèn)為 /data/data/<packageName>/files */ private void save2File() { try { //向文件寫入內(nèi)容 FileOutputStream os = openFileOutput("file.txt", Context.MODE_PRIVATE); String text = "寫數(shù)據(jù)到文件"; os.write(text.getBytes("utf-8")); //關(guān)閉流 os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * */ private void readFile() { try { FileInputStream ins = openFileInput("file.txt"); byte[] buffer = new byte[100]; int byteCount = ins.read(buffer); String text = new String(buffer,0,byteCount,"utf-8"); Toast.makeText(this,text,Toast.LENGTH_SHORT).show(); ins.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
文件存儲位置
/data/data/<package-name>/files
目錄下
openFileOutput和openFileInput方法可以獲得操作文件的OutputStream以及InputStream對象,而且可以通過流對象處理任何文件的數(shù)據(jù),但是這兩個(gè)方法同SharedPreferences一樣,只能在手機(jī)內(nèi)存卡的指定目錄建立文件,因此在使用上仍然有一定的局限性。
讀取SD卡上的文件
main_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.filedemo2.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清輸入文件名" /> <EditText android:id="@+id/edittitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="文件名" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清輸入文件內(nèi)容" /> <EditText android:id="@+id/editdetail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="文件內(nèi)容" /> <Button android:id="@+id/btnsave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存到SD卡" /> <Button android:id="@+id/btnclean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" /> <Button android:id="@+id/btnread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取sd卡中的文件" /> </LinearLayout>
接著我們來寫一個(gè)SD操作類: SDFileHelper.Java
public class SDFileHelper { private Context context; public SDFileHelper() { } public SDFileHelper(Context context) { super(); this.context = context; } //往SD卡寫入文件的方法 public void savaFileToSD(String filename, String filecontent) throws Exception { //如果手機(jī)已插入sd卡,且app具有讀寫sd卡的權(quán)限 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename; //這里就不要用openFileOutput了,那個(gè)是往手機(jī)內(nèi)存中寫數(shù)據(jù)的 FileOutputStream output = new FileOutputStream(filename); output.write(filecontent.getBytes()); //將String字符串以字節(jié)流的形式寫入到輸出流中 output.close(); //關(guān)閉輸出流 } else Toast.makeText(context, "SD卡不存在或者不可讀寫", Toast.LENGTH_SHORT).show(); } //讀取SD卡中文件的方法 //定義讀取文件的方法: public String readFromSD(String filename) throws IOException { StringBuilder sb = new StringBuilder(""); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename; //打開文件輸入流 FileInputStream input = new FileInputStream(filename); byte[] temp = new byte[1024]; int len = 0; //讀取文件內(nèi)容: while ((len = input.read(temp)) > 0) { sb.append(new String(temp, 0, len)); } //關(guān)閉輸入流 input.close(); } return sb.toString(); } }
接著MainActivity.java實(shí)現(xiàn)相關(guān)邏輯:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editname; private EditText editdetail; private Button btnsave; private Button btnclean; private Button btnread; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); bindViews(); } private void bindViews() { editname = (EditText) findViewById(R.id.edittitle); editdetail = (EditText) findViewById(R.id.editdetail); btnsave = (Button) findViewById(R.id.btnsave); btnclean = (Button) findViewById(R.id.btnclean); btnread = (Button) findViewById(R.id.btnread); btnsave.setOnClickListener(this); btnclean.setOnClickListener(this); btnread.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnclean: editdetail.setText(""); editname.setText(""); break; case R.id.btnsave: String filename = editname.getText().toString(); String filedetail = editdetail.getText().toString(); SDFileHelper sdHelper = new SDFileHelper(mContext); try { sdHelper.savaFileToSD(filename, filedetail); Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show(); } catch(Exception e){ e.printStackTrace(); Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入失敗", Toast.LENGTH_SHORT).show(); } break; case R.id.btnread: String detail = ""; SDFileHelper sdHelper2 = new SDFileHelper(mContext); try { String filename2 = editname.getText().toString(); detail = sdHelper2.readFromSD(filename2); } catch(IOException e){e.printStackTrace();} Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show(); break; } } }
最后別忘記在AndroidManifest.xml寫上讀寫SD卡的權(quán)限哦!
<!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard寫入數(shù)據(jù)權(quán)限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如何判斷虛擬和物理兩種SDK
在默認(rèn)情況下,會將一部分存儲空間分給虛擬的SD卡使用(一部分用于安裝Android操作系統(tǒng))
android.os.Enviroment.isExternalStorageRemovalbe()
返回true:SD卡是物理的,反之SD卡是虛擬的。
用于適配不同型號手機(jī),反射獲取SD卡路徑和狀態(tài)
package com.turing.base.activity.dataStore.fileStore; import android.content.Context; import android.os.Environment; import android.os.StatFs; import android.os.storage.StorageManager; import android.text.TextUtils; import android.util.Log; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 用于適配不同型號手機(jī),反射獲取SD卡路徑和狀態(tài) * */ public class DevMountInfo { private final String TAG = DevMountInfo.class.getSimpleName(); private static final int ERROR = -1; // class name private final static String CLASS_NAME = "android.os.storage.StorageVolume"; //remained spare memory size private static final int REMAINED_SPARE_IN_MB = 100; // method name private final static String METHOD_GET_VOLUME_LIST = "getVolumeList"; private final static String METHOD_GET_VOLUME_STATE = "getVolumeState"; private final static String METHOD_IS_REMOVABLE = "isRemovable"; private final static String METHOD_GET_PATH = "getPath"; private final static String MOUNTED = "mounted"; private static DevMountInfo INSTANCE; private String mSDCardPath = null; // internal file path private ConcurrentLinkedQueue<String> mInternalPathList = new ConcurrentLinkedQueue<String>(); // external file path private ConcurrentLinkedQueue<String> mExternalPathList = new ConcurrentLinkedQueue<String>(); private ExecutorService mExecutor = null; private DevMountInfo() { mExecutor = Executors.newSingleThreadExecutor(); } public static DevMountInfo getInstance() { synchronized (DevMountInfo.class) { if (null == INSTANCE) { INSTANCE = new DevMountInfo(); } return INSTANCE; } } @Override protected void finalize() throws Throwable { super.finalize(); synchronized (DevMountInfo.class) { mInternalPathList.clear(); mExternalPathList.clear(); mExecutor.shutdown(); INSTANCE = null; } } public void init(final Context context) { mExecutor.execute(new Runnable() { @Override public void run() { executeInit(context); } }); } public boolean isSDCardFull() { return REMAINED_SPARE_IN_MB > (getSDCardAvailSpace() * 1024); } public boolean isSDCardAvaiable() { return !mExternalPathList.isEmpty() || !mInternalPathList.isEmpty(); } public String getSDCardPath() { return mSDCardPath; } public long getSDCardTotalSpace() { long totalSpace = 0; if (!TextUtils.isEmpty(mSDCardPath)) { StatFs sf = new StatFs(mSDCardPath); long blockSize = sf.getBlockSize(); long total = sf.getBlockCount(); totalSpace = total * blockSize / 1024; } return totalSpace; } public long getSDCardAvailSpace() { long availSpace = 0; if (!TextUtils.isEmpty(mSDCardPath)) { StatFs sf = new StatFs(mSDCardPath); long blockSize = sf.getBlockSize(); long availCount = sf.getAvailableBlocks(); availSpace = availCount * blockSize / 1024; } return availSpace; } public String getInternalSDCardPath() { return mInternalPathList.peek(); } public String getExternalSDCardPath() { return mExternalPathList.peek(); } private void executeInit(Context context) { StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); if (mStorageManager != null) { Class<?> mStorageVolume = null; Method mGetVolumeListMethod = null; Method mGetVolumeStateMethod = null; Method mGetPathMethod = null; Method mIsRemovableMethod = null; Object[] mStorageVolumeList = null; try { mStorageVolume = Class.forName(CLASS_NAME); mGetVolumeListMethod = mStorageManager.getClass().getMethod(METHOD_GET_VOLUME_LIST, new Class[0]); mGetVolumeStateMethod = mStorageManager.getClass().getMethod(METHOD_GET_VOLUME_STATE, new Class[]{String.class}); mIsRemovableMethod = mStorageVolume.getMethod(METHOD_IS_REMOVABLE, new Class[0]); mGetPathMethod = mStorageVolume.getMethod(METHOD_GET_PATH, new Class[0]); mStorageVolumeList = (Object[]) mGetVolumeListMethod.invoke(mStorageManager, new Object[0]); boolean mIsRemovable = false; if (mStorageVolumeList != null && mStorageVolumeList.length > 0) { int mStorageVolumeCount = mStorageVolumeList.length; Log.i(TAG, "init() === > StorageVolume Count = " + mStorageVolumeCount); mInternalPathList.clear(); mExternalPathList.clear(); for (int i = 0; i < mStorageVolumeCount; ++i) { String mStoragePath = (String) mGetPathMethod.invoke(mStorageVolumeList[i], new Object[0]); mIsRemovable = ((Boolean) mIsRemovableMethod.invoke(mStorageVolumeList[i], new Object[0])).booleanValue(); if (!TextUtils.isEmpty(mStoragePath)) { String state = (String) mGetVolumeStateMethod.invoke(mStorageManager, new Object[]{mStoragePath}); if ((state != null) && (state.equals(MOUNTED))) { if (mIsRemovable) { Log.i(TAG, "init() === > external storage path = (" + mStoragePath + ")"); mExternalPathList.add(mStoragePath); } else { Log.i(TAG, "init() === > internal storage path = (" + mStoragePath + ")"); mInternalPathList.add(mStoragePath); } } } } } } catch (ClassNotFoundException e) { handleInvalid(); Log.e(TAG, "init() === > Exception:ClassNotFoundException"); } catch (NoSuchMethodException e) { handleInvalid(); Log.e(TAG, "init() === > Exception:NoSuchMethodException"); } catch (IllegalArgumentException e) { handleInvalid(); Log.e(TAG, "init() === > Exception:IllegalArgumentException"); } catch (IllegalAccessException e) { handleInvalid(); Log.e(TAG, "init() === > Exception:IllegalAccessException"); } catch (InvocationTargetException e) { handleInvalid(); Log.e(TAG, "init() === > Exception:InvocationTargetException"); } } else { handleInvalid(); Log.e(TAG, "init() === > can't get storage manager"); } initSDCardPath(); } private void handleInvalid() { mInternalPathList.add(Environment.getExternalStorageDirectory().getPath()); } private void initSDCardPath() { if (!mExternalPathList.isEmpty()) { mSDCardPath = mExternalPathList.peek(); } else if (!mInternalPathList.isEmpty()) { mSDCardPath = mInternalPathList.peek(); } else { mSDCardPath = Environment.getExternalStorageDirectory().getPath(); } Log.i(TAG, "initSDCardPath() === > SDCARD PATH = (" + mSDCardPath + ")"); } /** * SDCARD是否存 */ public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } /** * 獲取手機(jī)內(nèi)部剩余存儲空間 * * @return */ public static long getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } /** * 獲取手機(jī)內(nèi)部總的存儲空間 * * @return */ public static long getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } /** * 獲取手機(jī)內(nèi)置存儲剩余存儲空間 * * @return */ public static long getAvailableInternalSystemMemorySize() { File path = Environment.getRootDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } /** * 獲取手機(jī)內(nèi)置存儲總的存儲空間 * * @return */ public static long getTotalInternalSystemMemorySize() { File path = Environment.getRootDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } /** * 獲取SDCARD剩余存儲空間 * * @return */ public static long getAvailableExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } else { return ERROR; } } /** * 獲取SDCARD總的存儲空間 * * @return */ public static long getTotalExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } else { return ERROR; } } public static long getAvailableMemorySize(String path) { if (null == path) return 0; StatFs stat = new StatFs(path); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } }
讀取raw和assets文件夾下的文件
相信大家對兩個(gè)文件夾并不陌生,如果我們不想自己的文件被編譯成二進(jìn)制文件的話, 我們可以把文件放到這兩個(gè)目錄下,而兩者的區(qū)別如下:
- res/raw:文件會被映射到R.java文件中,訪問的時(shí)候直接通過資源ID即可訪問,而且 他不能有目錄結(jié)構(gòu),就是不能再創(chuàng)建文件夾
- assets:不會映射到R.java文件中,通過AssetManager來訪問,能有目錄結(jié)構(gòu),即, 可以自行創(chuàng)建文件夾。
讀取文件資源:
res/raw:
InputStream is =getResources().openRawResource(R.raw.filename);
assets:
AssetManager am = getAssets(); InputStream is = am.open("filename");
SAX引擎讀取XML文件
sax引擎讀取xml文件的原理:
sax技術(shù)在處理xml文件時(shí)并不一次性把xml文件裝入內(nèi)存,而是一邊讀一般解析。
使用sax處理xml需要一個(gè)Handler對象,一般會使用org.xml.sax.helpers.DefaultHandler的子類作為Handler對象
因此,這就需要處理如下5個(gè)分析點(diǎn),也可稱為分析事件:
- 開始分析xml文件。該分析點(diǎn)表示sax引擎剛開始處理xml文件,還沒有讀取xml文件中的內(nèi)容。該分析點(diǎn)對應(yīng)于DefaultHandler類中的startDocument()事件方法,可以在該方法中做一下初始化的工作!
- 開始處理每一個(gè)xml元素,也就是遇到<product>,<item>這樣的起始標(biāo)記,sax引擎每次掃描到新的xml元素的起始標(biāo)記會觸發(fā)這個(gè)分析事件,對應(yīng)的事件分析方法是startElement,在該方法中可以獲取當(dāng)前元素的名稱和元素屬性的相關(guān)信息
- 處理完一個(gè)xml元素,也就是遇到</product>,</item>這樣的結(jié)束標(biāo)記,該分析點(diǎn)對應(yīng)的事件方法是endElement,在該事件中可以獲得當(dāng)前處理完的元素的全部信息。
- 處理完xml文件。如果sax引擎將整個(gè)xml文件的內(nèi)容都掃描完了,就到了這個(gè)分析點(diǎn),該分析點(diǎn)對應(yīng)的事件方法endDocument(),該事件方法可能不是必需的,如果最后有以下收尾工作,如釋放一下資源,可以在該方法中完成!
- 讀取字符分析點(diǎn)。這是最重要的分析點(diǎn)。如果沒有這個(gè)分析點(diǎn),前4步的處理相當(dāng)于白跑一遍,雖然讀取了xml文件中的所有內(nèi)容,但并未保存這些內(nèi)容,而這個(gè)分析點(diǎn)所對應(yīng)的characters事件方法的主要作用就是保存sax引擎讀取的xml文件中的內(nèi)容。更準(zhǔn)確地說是保存xml元素的文本,也就是<product>abc</product>中的abc。
Code
res\raw\product.xml
<?xml version="1.0" encoding="utf-8"?> <products> <product> <id>10</id> <name>電腦</name> <price>2067.25</price> </product> <product> <id>20</id> <name>微波爐</name> <price>520</price> </product> <product> <id>30</id> <name>洗衣機(jī)</name> <price>2400</price> </product> </products>
Product.java
public class Product { private int id; private String name; private float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }
XML2Product.java(DefaultHandler子類)
DefaultHandler子類 ,核心類,負(fù)責(zé)處理分析點(diǎn)事件。
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import java.util.ArrayList; import java.util.List; public class XML2Product extends DefaultHandler { private List<Product> products; private Product product; private StringBuffer buffer = new StringBuffer(); public List<Product> getProducts() { return products; } @Override public void characters(char[] ch, int start, int length) throws SAXException { buffer.append(ch, start, length); super.characters(ch, start, length); } @Override public void startDocument() throws SAXException { // 開始分析xml文件,創(chuàng)建List對象用于保存分析完的Product對象 products = new ArrayList<Product>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals("product")) { // 如果分析的是<product>標(biāo)簽,則創(chuàng)建一個(gè)Product對象 product = new Product(); } super.startElement(uri, localName, qName, attributes); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (localName.equals("product")) { // 處理完 <product>標(biāo)簽后 將product對象添加到products中 products.add(product); } else if (localName.equals("id")) { // 設(shè)置id屬性的值 product.setId(Integer.parseInt(buffer.toString().trim())); // 將標(biāo)簽內(nèi)容的緩存區(qū)清空 buffer.setLength(0); } else if (localName.equals("name")) { product.setName(buffer.toString().trim()); buffer.setLength(0); } else if (localName.equals("price")) { product.setPrice(Float.parseFloat(buffer.toString().trim())); buffer.setLength(0); } super.endElement(uri, localName, qName); } }
Xml2JavaObjectAct
import android.app.AlertDialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Xml; import android.view.View; import com.turing.base.R; import java.io.InputStream; import java.util.List; public class Xml2JavaObjectAct extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xml2_java_object); } public void onClick_XMLToObject(View view) { try { // 打開資源文件 InputStream is = getResources().openRawResource(R.raw.products); XML2Product xml2Product = new XML2Product(); // 開始分析priducts.xml文件 android.util.Xml.parse(is, Xml.Encoding.UTF_8, xml2Product); // 輸出轉(zhuǎn)換后的java對象 List<Product> products = xml2Product.getProducts(); String msg = "共" + products.size() + "個(gè)產(chǎn)品\n"; for (Product product : products) { msg += "id:" + product.getId() + " 產(chǎn)品名:" + product.getName() + " 價(jià)格:" + product.getPrice() + "\n"; } // 彈出對話框 new AlertDialog.Builder(this).setTitle("產(chǎn)品信息").setMessage(msg) .setPositiveButton("關(guān)閉", null).show(); } catch (Exception e) { } } }
效果圖
Code
activity_jar_zip.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Jar_Compress" android:text="用jar格式壓縮文件" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Jar_Uncompress" android:text="解壓jar格式文件" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Zip_Compress" android:text="用zip格式壓縮文件" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Zip_Uncompress" android:text="解壓zip格式文件" /> </LinearLayout>
JarZipAct
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.turing.base.R; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class JarZipAct extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jar_zip); } public void onClick_Jar_Compress(View view) { try { // 使用FileOutputStream對象指定一個(gè)要輸出的壓縮文件(file.jar) FileOutputStream fos = new FileOutputStream( android.os.Environment.getExternalStorageDirectory() + "/file.jar"); // 第一步 創(chuàng)建JarOutputStream對象 JarOutputStream jos = new JarOutputStream(fos); // 第二步 創(chuàng)建一個(gè)JarEntry對象,并指定待壓縮文件在壓縮包中的文件名 JarEntry jarEntry = new JarEntry("strings.xml"); jos.putNextEntry(jarEntry); InputStream is = getResources().getAssets().open("strings.xml"); byte[] buffer = new byte[8192]; int count = 0; // 第四步 寫入數(shù)據(jù) while ((count = is.read(buffer)) >= 0) { jos.write(buffer, 0, count); } // 第五步 關(guān)閉當(dāng)前的JarEntry等對象 is.close(); jos.closeEntry(); jos.close(); Toast.makeText(this, "成功將strings.xml文件以jar格式壓縮.", Toast.LENGTH_LONG) .show(); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } public void onClick_Jar_Uncompress(View view) { try { // 定義要解壓的文件 String filename = android.os.Environment .getExternalStorageDirectory() + "/file.jar"; if (!new File(filename).exists()) { Toast.makeText(this, "壓縮文件不存在.", Toast.LENGTH_LONG).show(); return; } // 使用FileInputStream對象指定要解壓的對象 FileInputStream fis = new FileInputStream(filename); // 1 創(chuàng)建JarInputStream對象來讀取壓縮文件(file.jar) JarInputStream jis = new JarInputStream(fis); // 2 調(diào)用getNextJarEntry方法打開壓縮包中的第一個(gè)文件 ,如果有多個(gè),多次調(diào)用該方法 JarEntry jarEntry = jis.getNextJarEntry(); // 3 輸出已解壓的文件 FileOutputStream fos = new FileOutputStream( android.os.Environment.getExternalStorageDirectory() + "/" + jarEntry.getName()); byte[] buffer = new byte[8192]; int count = 0; // 4 輸出已解壓的字節(jié)流 while ((count = jis.read(buffer)) >= 0) { fos.write(buffer, 0, count); } // 5 關(guān)閉 jis.closeEntry(); jis.close(); fos.close(); Toast.makeText(this, "成功解壓jar格式的文件.", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } public void onClick_Zip_Compress(View view) { try { // 指定了2個(gè)待壓縮的w文件,都在assets目錄中 String[] filenames = new String[] {"main.xml", "strings.xml"}; FileOutputStream fos = new FileOutputStream( android.os.Environment.getExternalStorageDirectory() + "/file.zip"); ZipOutputStream zos = new ZipOutputStream(fos); int i = 1; //枚舉filenames中的所有待壓縮文件 while (i <= filenames.length) { // 從filenames數(shù)組中取出當(dāng)前待壓縮的溫佳明,作為壓縮后的文件名,以保持要說前后文件名稱一致 ZipEntry zipEntry = new ZipEntry(filenames[i - 1]); // 打開當(dāng)前的ZipEntry對象 zos.putNextEntry(zipEntry); InputStream is = getResources().getAssets().open( filenames[i - 1]); byte[] buffer = new byte[8192]; int count = 0; // 寫入數(shù)據(jù) while ((count = is.read(buffer)) >= 0) { zos.write(buffer, 0, count); } zos.flush(); // 關(guān)閉當(dāng)前的ZipEntry對象 zos.closeEntry(); is.close(); i++; } zos.finish(); zos.close(); Toast.makeText(this, "成功將main.xml、strings.xml文件以zip格式壓縮.", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } public void onClick_Zip_Uncompress(View view) { try { // 指定待解壓的文件 String filename = android.os.Environment .getExternalStorageDirectory() + "/file.zip"; if (!new File(filename).exists()) { Toast.makeText(this, "壓縮文件不存在.", Toast.LENGTH_LONG).show(); return; } FileInputStream fis = new FileInputStream(filename); ZipInputStream zis = new ZipInputStream(fis); ZipEntry zipEntry = null; // 通過不斷調(diào)用getNextEntry方法來解壓file.zip中所有的文件 while ((zipEntry = zis.getNextEntry()) != null) { FileOutputStream fos = new FileOutputStream( android.os.Environment.getExternalStorageDirectory() + "/" + zipEntry.getName()); byte[] buffer = new byte[8192]; int count = 0; while ((count = zis.read(buffer)) >= 0) { fos.write(buffer, 0, count); } zis.closeEntry(); fos.close(); } zis.close(); Toast.makeText(this, "成功解壓jar格式的文件.", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } }
原文鏈接:http://blog.csdn.net/yangshangwei/article/details/50831269
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android數(shù)據(jù)存儲之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲的思考
- Android實(shí)現(xiàn)文件存儲并讀取的示例代碼
- android開發(fā)基礎(chǔ)教程—文件存儲功能實(shí)現(xiàn)
- Android圖片添加水印圖片并把圖片保存到文件存儲的實(shí)現(xiàn)代碼
- 實(shí)例詳解Android文件存儲數(shù)據(jù)方式
- 詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(附源碼)
- Android學(xué)習(xí)之文件存儲讀取
- 詳解Android文件存儲
- Android編程之SharedPreferences文件存儲操作實(shí)例分析
- Android開發(fā)文件存儲實(shí)例
相關(guān)文章
Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的功能
這篇文章主要介紹了Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的功能的相關(guān)資料,開發(fā)Android應(yīng)用的朋友肯定對自定義View不陌生,很多都有重新寫的,這里就對實(shí)現(xiàn)圓形進(jìn)度條介紹下,需要的朋友可以參考下2016-11-11Android UI設(shè)計(jì)與開發(fā)之實(shí)現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之實(shí)現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說句人話就是:簡化自定義View構(gòu)造參數(shù)模板代碼的編寫,實(shí)現(xiàn)自動生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

Android 簡單實(shí)現(xiàn)倒計(jì)時(shí)功能

解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見問題

Android 實(shí)現(xiàn)滑動方法總結(jié)