Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android 文件 File 讀寫

Android 文件 File 讀寫

編輯:初級開發

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. 運行結果:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved