編輯:初級開發
File 讀寫
[功能]
因為文件讀寫很平常 使用打算把這個功能寫出輔助類的形式 以便以後方便使用 就是:FileIOHelper
[代碼]
1. 定義指定的File 以及其上的 FileInputStream FileOutputStream
Java代碼
Context context;
File file;
FileInputStream fin;
FileOutputStream fout;
public FileIOHelper(Context c, String name,String path) throws IOException{
context = c;
file = new File(path,name);
file.createNewFile();
fin = new FileInputStream(file);
fout = new FileOutputStream(file);
}
Context context;
File file;
FileInputStream fin;
FileOutputStream fout;
public FileIOHelper(Context c, String name,String path) throws IOException{
context = c;
file = new File(path,name);
file.createNewFile();
fin = new FileInputStream(file);
fout = new FileOutputStream(file);
}
2. 文件寫
Java代碼
public void wrire(String s) throws IOException{
fout.write(s.getBytes());
fout.close();
}
public void wrire(String s) throws IOException{
fout.write(s.getBytes());
fout.close();
}
3. 文件讀
Java代碼
public byte[] read(int s,int l) throws IOException{
byte[] save = new byte[l];
fin.read(save, s, l);
return save;
}
public byte[] read(int s,int l) throws IOException{
byte[] save = new byte[l];
fin.read(save, s, l);
return save;
}
4. 編碼轉換
Java代碼
public String encode(byte[] array){
return EncodingUtils.getString(array,TEXT_ENCODING);
}
public String encode(byte[] array){
return EncodingUtils.getString(array,TEXT_ENCODING);
}
5. 文件長度
Java代碼
public int length(){
return (int)file.length();
}
public int length(){
return (int)file.length();
}
6. 如何使用FileIOHelper
Java代碼
public class FileIOUsage extends Activity {
public final static String NAME = "griffinshi.txt";
public final static String PATH = "data/data/com.android.FileIO";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentVIEw(R.layout.main);
try {
FileIOHelper helper = new FileIOHelper(this,NAME,PATH );
String string = "Hello to Gryphone!";
helper.wrire(string);
byte[] array = helper.read(0, helper.length());
String data = helper.encode(array);
TextView tv = new TextVIEw(this);
tv.setText(data);
setContentVIEw(tv);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class FileIOUsage extends Activity {
public final static String NAME = "griffinshi.txt";
public final static String PATH = "data/data/com.android.FileIO";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentVIEw(R.layout.main);
try {
FileIOHelper helper = new FileIOHelper(this,NAME,PATH );
String string = "Hello to Gryphone!";
helper.wrire(string);
byte[] array = helper.read(0, helper.length());
String data = helper.encode(array);
TextView tv = new TextVIEw(this);
tv.setText(data);
setContentVIEw(tv);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
7. 其他待解決問題
* 文件似乎只支持一次寫 而不支持追加操作 不知何故 以後有時間在回頭看看
8. 生成的文件:
Java代碼
E:android-devsdkandroid-sdk-Windows-1.5_r2 ools>adb shell
# cd data
cd data
# cd data
cd data
# cd com.android.FileIO
cd com.android.FileIO
# ls
ls
griffinshi.txt
lib
E:android-devsdkandroid-sdk-Windows-1.5_r2 ools>adb shell
# cd data
cd data
# cd data
cd data
# cd com.android.FileIO
cd com.android.FileIO
# ls
ls
griffinshi.txt
lib
9. 運行結果:
在游戲開發中,往往要提供選關的頁面,選擇關卡可以簡單地使用listVIEw,如果想效果好一點,可以選擇 用gallery控件。Gallery控件的使用在api demo
摘自:http://www.eoeandroid.com/thread-15411-1-1.HtmlFailed to push XXXXX.txt on emulat
1. 先定義XML布局文件,<SeekBar android:id=@+id/mySeek
移動開發大潮興起於蘋果公司的iPhone,移動浏覽器也隨之得到迅猛發展,特別是對Html5、CSS3以及CSS動畫的支持。之後Google公司推出android更是開啟