編輯:關於Android編程
openFileOutput() with
the name of the file and the operating mode. This returns a FileOutputStream.通過 openFileOutput()建立FileoutputStream對象write().創建Write對象並進行數據讀寫操作close().最後關閉鏈接
以上就是講數據文件保存到內部儲存的基本步驟
下面用代碼進行一下講解
package com.example.internalstorage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button saveData, getData;
private static final String FILENAME = "flyou.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saveData=(Button)this.findViewById(R.id.button1);
getData=(Button)this.findViewById(R.id.button2);
saveData.setOnClickListener(new View.OnClickListener() {//保存信息
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FileOutputStream out = null;
try {
String infoString = "風飛雪未揚";
out = openFileOutput(FILENAME, Context.MODE_PRIVATE);
out.write(infoString.getBytes());
Toast.makeText(MainActivity.this, "數據保存成功", 2).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (out!=null) {
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
// 讀取信息
getData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FileInputStream input=null;
try {
input=openFileInput(FILENAME);
byte[] data=new byte[1024];
int len=input.read(data);
input.close();
// System.out.println(new String(data,0,len));
Toast.makeText(MainActivity.this, new String(data,0,len), 2).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

打開信息
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140829/20140829091144210.png" alt="\">
使用文件浏覽器查看信息


將文件導出

用記事本打開

以上操作可以很容易的實現文件的內部存儲過程
官方api當中還有關於如何存儲緩存文件的說明,大家可以自行實驗一下
下節預報:
Android網絡編程(總結)
概述首先,應該了解的幾個問題:1)Android平台網絡相關API接口a) java.net.*(標准Java接口) java.net.*提供與聯網有關的類,包
簡單談談android studio 的單元測試
面對android studio Run 一次項目要等好幾分鐘的痛點,不得不研究一下android studio 的單元測試。其實我的目的很簡單,在不對視圖進行操作的前提
OKHTTP 的使用完全解析
一、前言在Android客戶端開發中,使用網絡請求是非常常見的事情,一般我們使用HttpURLConnection是可以滿足需求的,不過隨著業務邏輯復雜,依然還是有很多不
Android App在線程中創建handler的方法講解
相關概念1.Handler:可以看做是一個工具類,用來向消息隊列中插入消息的;2.Thread:所有與Handler相關的功能都是與Thread密不可分的,Handler