編輯:關於Android編程
原理就是利用java的IO。
openFileOutput()方法的第一參數用於指定文件名稱,不能包含路徑分隔符“/” ,如果文件不存在,Android 會自動創建它。創建的文件保存在/data/data/
可以通過File Explorer查看。點擊右上角的可以導出到電腦裡。
openFileOutput()方法的第二參數用於指定操作模式
私有操作模式創建出來的文件只能被本應用所訪問,其他應用無法方法該文件,另外采用私有操作模式創建的文件,寫入文件中的內容會覆蓋原文件的內容
package com.example.service;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
/**
* 保存文件
* @param filename 文件名稱
* @param filecontent 文件內容
* @throws Exception
*/
public void save(String filename, String filecontent) throws Exception {
FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(filecontent.getBytes());
outputStream.close();
}
/**
* 讀取文件內容
* @param fileName 文件名稱
* @return 文件內容
* @throws Exception
*/
public String read(String fileName) throws Exception{
FileInputStream instream = context.openFileInput(fileName);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = instream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
byte []data = outputStream.toByteArray();
return new String(data);
}
}
華為p9plus雙清手機方法介紹
華為P9Plus雙清手機方法。相信很多網友都需要清理手機,或因為內存滿了,或忘記密碼了,不管是什麼緣由,雙清手機都能達到你想要的要求,那麼華為p9plus怎
Android官方開發文檔Training系列課程中文版:通知用戶之大視圖通知
原文地址:http://android.xsoftlab.net/training/notify-user/expanded.html通知在通知欄中以兩種風格呈現:正常視
Android MVP 十分鐘入門!
前言在日常開發APP 的過程中,隨著業務的擴展,規模的變化。我們的代碼規模也會逐漸變得龐大,每一個類裡的代碼也會逐漸增多。尤其是Activity和Fragment ,由於
Android中的全局變量與局部變量使用小結
全局變量顧名思義就是在整個的類中或者可在多個函數中調用的變量。也稱為外部變量。局部變量則是特定過程或函數中可以訪問的變量。聲明一個變量是很 容易的,但是講到使用的時候,卻