編輯:關於Android編程
(1)目錄結構

(2) 布局文件:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">
(3)保存數據、讀取數據的工具類:FileService.java
package com.example.data_storage_interal.file;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
/**
* 使用內部存儲的方式,可以使應用卸載的時候清除所有的相關信息
*
* @author piaodangdehun
*
*/
public class FileService {
private Context context;
public FileService(Context context) {
this.context = context;
}
/**
* 保存內容到文件中
*
* @param fileName
* : 文件名
* @param mode
* :模式
* @param data
* :數據的緩沖區
* @return 返回真假值
*/
public boolean saveContentToFile(String fileName, int mode, byte[] data) {
boolean flag = false;
FileOutputStream outputStream = null;
try {
outputStream = context.openFileOutput(fileName, mode);
try {
outputStream.write(data, 0, data.length);
} catch (IOException e) {
e.printStackTrace();
}
flag = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e2) {
}
}
}
return true;
}
/**
* 讀取數據
*
* @param fileName
* @return
*/
public String readContentFromFile(String fileName) {
String result = "";
FileInputStream fileInputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
fileInputStream = context.openFileInput(fileName);
int len = 0;
byte[] data = new byte[1024];
while ((len = fileInputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
return new String(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/*
* 保存文件到Chace中
*/
public boolean saveCacheFile(String fileName, byte[] data) {
boolean flag = false;
File file = context.getFilesDir();
FileOutputStream outputStream = null;
try {
File folderFile = new File(file.getAbsoluteFile() + "/txt");
if (!folderFile.exists()) {
folderFile.mkdirs();// 創建目錄
}
// context.openFileOutput("my.txt", context.MODE_WORLD_READABLE);
outputStream = new FileOutputStream(folderFile.getAbsolutePath()
+ "/" + fileName);
try {
outputStream.write(data, 0, data.length);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// System.out.println("-->>"+file.getAbsolutePath());
return flag;
}
/*
* 獲得文件的完整路徑
*/
public void listCacheFile() {
/*
* String[] strings = context.fileList(); for (int i = 0; i <
* strings.length; i++) { System.out.println("-->>" + strings); }
*/
File file = context.getFilesDir();
File root = new File(file.getAbsoluteFile() + "/txt");
File[] listFiles = root.listFiles();
for (File file2 : listFiles) {
System.err.println("--->>" + file2.getAbsolutePath());
}
}
}
package com.example.data_storage_interal;
import com.example.data_storage_interal.file.FileService;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
private EditText editText;
private FileService fileService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
editText = (EditText) this.findViewById(R.id.editText1);
fileService = new FileService(this);
/*
* 按鈕點擊的時候存放到文件中
*/
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String valueString = editText.getText().toString().trim();
boolean flag = fileService.saveContentToFile("bb.txt",
Context.MODE_APPEND, valueString.getBytes());
if (flag) {
Toast.makeText(MainActivity.this, "保存成功!",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
(5)測試類(要在資源文件中添加相應的信息、這裡不說)
package com.example.data_storage_interal;
import com.example.data_storage_interal.file.FileService;
import android.app.Service;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
public class MyTest extends AndroidTestCase {
private static final String TAG = "MyTest";
/*
* 保存數據
*/
public void save() {
FileService fileService = new FileService(getContext());
boolean flag = fileService.saveContentToFile("aa.txt",
Context.MODE_PRIVATE + Context.MODE_APPEND, "nihao".getBytes());
Log.i(TAG, "--->>" + flag);
}
/*
* 保存數據
*/
public void read() {
FileService fileService = new FileService(getContext());
String msg = fileService.readContentFromFile("bb.txt");
Log.i(TAG, "--->>" + msg);
}
public void testChaceFile() {
FileService fileService = new FileService(getContext());
fileService.saveCacheFile("hello.txt", "hello world!".getBytes());
}
public void testListFile() {
FileService fileService = new FileService(getContext());
fileService.listCacheFile();
}
}

保存成功後會在data-data-apk的安裝目錄下找到相應的文件:

Android ORM 框架之 greenDAO
我相信,在平時的開發過程中,大家一定會或多或少地接觸到 SQLite。然而在使用它時,我們往往需要做許多額外的工作,像編寫 SQL 語句與解析查詢結果等。所以,適用於 A
實例探究Android應用編寫時Fragment的生命周期問題
管理fragment的生命周期有些像管理activity的生命周期。Fragment可以生存在三種狀態:Resumed:Fragment在一個運行中的activity中並
橙子VR怎麼樣 橙子vr支持什麼格式 橙子VR使用教程
橙子VR app是一款提供3D/全景視頻播放、VR游戲下載、VR熱點資訊的虛擬現實內容聚合平台。橙子VR app是一個優質VR視頻3D電影電視劇動
Android編程實現讀取手機聯系人、撥號、發送短信及長按菜單操作方法實例小結
本文實例講述了Android編程實現讀取手機聯系人、撥號、發送短信及長按菜單操作方法。分享給大家供大家參考,具體如下:1.Andrid項目結構圖↓主要操作圖中紅色方框內的