編輯:關於Android編程
關於Android文件的讀寫有兩種方式
一種是將txt文件當成資源文件放在res/raw或則res/asset文件夾下,raw的文件可以通過R.raw.fileName獲得,asset下的文件可以通過AssetManager am = getAssets();am.open(“FileName”);來打開文件。但是如果把文件當成資源文件存放的話,只能讀不能寫。如果要想寫入數據的話,就使用第二種方法
第二種方法從sd卡中讀寫文件,這樣首先要向AndroidManifest.xml中加入兩條權限消息
package com.example.littleapplication;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Vector;
import android.os.Environment;
import android.util.Log;
public class FileOption {
private String fileName;
private File targetFile;
private File sdCardDir;
public FileOption(String fileName) throws IOException {
// TODO Auto-generated constructor stub
boolean mark = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(mark)
{
this.fileName = fileName;
this.sdCardDir = Environment.getExternalStorageDirectory();
this.targetFile = new File(this.sdCardDir.getCanonicalPath()+this.fileName);
if(this.targetFile.exists()==false)
{
this.targetFile.createNewFile();
}
}
else{
Log.e(SDK, 無內存卡);
}
}
public Vector read() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(this.sdCardDir.getCanonicalPath()+this.fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
Vector res = new Vector();
String line = null;
while((line = br.readLine())!=null)
{
res.add(line);
}
br.close();
return res;
// TODO Auto-generated method stub
}
public boolean write(String add) throws IOException
{
RandomAccessFile raf = new RandomAccessFile(this.targetFile,rw);
raf.seek(targetFile.length());
//換行
raf.write(add.getBytes());
raf.writeChar('
');
raf.close();
return true;
}
/**返回一共有多少條記錄
* @throws IOException
* @throws FileNotFoundException */
public int size() throws FileNotFoundException, IOException
{
FileInputStream fis = new FileInputStream(this.sdCardDir.getCanonicalPath()+this.fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine())!=null)
{
count++;
}
//每三行代表一個數據項
//分別是 id title content data level
return count/5;
}
}
Android—基於微信開放平台v3SDK開發(微信支付填坑)
接觸微信支付之前聽說過這是一個坑,,,心裡已經有了准備。。。我以為我沒准跳坑出不來了,沒有想到我填上了,調用成功之後我感覺公司所有的同事都是漂亮的,隔著北京的大霧霾我仿佛
Android之同一個TextView設置不同樣式的文字
需求分析:很多時候,我們需要在視圖中顯示不同樣式的文字,但是為了減少viewgroup層級,不想新增很多個TextView控件來實現不同樣式的文字。那麼有沒有一種方式能夠
Android筆記八.Intent對象的使用及.apk反編譯
Intent對象的使用及.apk反編譯 Jiangdg_VIP http://blog.csdn.net/u012637501 前三節主要學習了Inte
安卓 .9.png圖片寫聊天對話框的 詳細教程
最近在學Android 學的不好 然後看到了用.9.png寫對話框的哪裡,但是書上寫的太簡單 感覺做出來和書上的不一樣 然後就去各種百度 感覺網上關於這個東西的資料都是粘