Android学习一:文件操作
最近在學習安卓相關的編程,對于一門新技術的學習,我想應該跟其他語言一樣吧,如C++和C#,也是文件,網絡,多線程以及數據庫之類的學習了。所以決定先從文件下手,現在將文件的一點學習列在下面:
1.Java中文件的操作
Java中文件的操作分為兩種類型:字節型(Stream)和字符型(Reader),但是字符型在處理非文本類型的數據時會有問題。
好了上一段代碼:
/*** 文件的復制* @param srcPath 源文件路徑* @param destPath 目標文件路徑* @throws Exception 拋出的異常*/public static void copy(String srcPath, String destPath) throws Exception{File src = new File(srcPath);File dest = new File(destPath);InputStream input = new BufferedInputStream(new FileInputStream(src));OutputStream output = new BufferedOutputStream(new FileOutputStream(dest));int len = 0;byte[] buffer = new byte[1024];while(-1 != (len = input.read(buffer))){output.write(buffer,0,len);}output.close();input.close();}?
這段代碼的主要功能是復制源文件到目標文件,需要特別注意的是,目標文件必須指定文件名,如果是路徑的話,會出錯。
File src = new File(srcPath); File dest = new File(destPath);這兩句話的意思是與文件建立聯系,以便讓后面的流進行操作,當然也可以直接在流中指定文件的路徑。
?
InputStream input = new BufferedInputStream(new FileInputStream(src));這邊就是聲明輸出流了,因為是將外部文件導入到程序中,所以需要使用FileInputStream,這也可以理解。那么BufferedInputStream是有什么作用呢?使用BufferedInputStream對FileInputStream進行裝飾,使普通的文件輸入流具備了內存緩存的功能,通過內存緩沖減少磁盤IO次數,看到別人博客上的一句話,說的清晰明了就拿過來用了。對于BufferedOutputStream,其作用是類似的。
另外,在input.read()處理到文件末尾的時候就會返回-1,以此來判斷文件是否讀取結束。對于output.close()內部會調用flush方法將緩存的數據寫進文件,最后記得關閉流。
在這里關于異常的是直接拋出的,因為這個方法是邏輯層,所以異常處理交給業務層。
?
2.Android文件的操作
public class FileService {private FileService() {}public static void write(String fileName, String content) throws Exception { File file = new File(fileName);// 建立連接 OutputStream output = new BufferedOutputStream(new FileOutputStream( file)); output.write(content.getBytes()); output.close(); } }既然在Java中可以使用該代碼,那么在android中應這段代碼就應該是可用的。但是在執行的時候發生了錯誤:
打開文件錯誤:只讀文件系統 open failed:EROFS(read only file system)
這是什么原因呢?后來在stack overflow上找到了下面一段話:
You will not have access to the file-system root, which is what you're attempting to access. For your purposes, you can write to internal files new File("test.png"), which places the file in the application-internal storage -- better yet, access it explicitly using getFilesDir().
大致意思是,app沒有系統root目錄的權限,也就是說,我直接創建file的時候,是在系統的根目錄上創建的,然而所寫的App沒有相關的權限。但是可以寫在手機的內部存儲器上,通過getFilesDir()來獲取內部的目錄。更新代碼如下:
package org.tonny.util;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.OutputStream; import android.content.Context; public final class FileService { private Context context; public FileService(Context context) { this.context = context; } public void write(String fileName, String content) throws Exception { File file = new File(context.getFilesDir() + File.separator + fileName);// 建立連接 OutputStream output = new BufferedOutputStream(new FileOutputStream( file)); output.write(content.getBytes()); output.close(); } }這樣修改之后,果然可以創建文件了。那么android是否提供了另外的方法來進行相關的操作了,經過一番查閱,發現還是有的,代碼如下:
?
package org.tonny.util;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.OutputStream;import android.content.Context;public final class FileService {private Context context;public FileService(Context context) {this.context = context;}public void write(String fileName, String content) throws Exception {File file = new File(context.getFilesDir() + File.separator + fileName);// 建立連接OutputStream output = new BufferedOutputStream(new FileOutputStream(file));output.write(content.getBytes());output.flush();output.close();}public void writeFile(String fileName, String content) throws Exception{OutputStream output = new BufferedOutputStream(context.openFileOutput(fileName, Context.MODE_PRIVATE));output.write(content.getBytes());output.flush();output.close();}}同樣是可以在app相應的目錄下創建文件。
那么可以在文件的內部存儲器存放文件,如何在SD卡上存放文件呢,網上的例子還是比較多的,代碼如下:
public void writeSDFile(String fileName, String content) throws Exception {boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);if (!sdCardExist) {// SD卡不存在,則退出return;}File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + fileName);OutputStream output = new BufferedOutputStream(new FileOutputStream(file));output.write(content.getBytes());output.flush();output.close();}這里需要先判斷一下SD卡是否存在,存在才能進一步操作,同時需要在清單文件中配置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>代碼是寫好了,簡單的重構一下:
package org.tonny.util;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream;import android.content.Context; import android.os.Environment;public final class FileService {private Context context;public FileService(Context context) {this.context = context;}public void write(String fileName, String content) throws Exception {File file = new File(context.getFilesDir() + File.separator + fileName);// 建立連接 oper(file, content);}public void writeFile(String fileName, String content) throws Exception {OutputStream output = new BufferedOutputStream(context.openFileOutput(fileName, Context.MODE_PRIVATE));output.write(content.getBytes());output.flush();output.close();}public void writeSDFile(String fileName, String content) throws Exception {boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);if (!sdCardExist) {// SD卡不存在,則退出return;}File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + fileName);oper(file, content);}/*** 文件寫操作* * @param file* @param content* @throws Exception*/private void oper(File file, String content) throws Exception {OutputStream output = new BufferedOutputStream(new FileOutputStream(file));output.write(content.getBytes());output.flush();output.close();} }?
由于學習還在一個較淺的層面,所以就寫了這幾個簡單的例子。還有很多內容有待豐富,如文件的屬性獲取,復制以及讀取等操作都沒有寫。另外沒有從整體上介紹Android IO操作類的結構,所以文章還是有些混亂的。哎,第一次寫,時間花了不少,休息啦。?
?
轉載于:https://www.cnblogs.com/supertonny/p/4401214.html
總結
以上是生活随笔為你收集整理的Android学习一:文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SH1B LMR62014XMFE/NO
- 下一篇: JVM学习笔记:Java运行时数据区域