Android讀寫Txt文字檔

來源:互聯網
上載者:User

在Android平台中經常要用到Txt文字檔的讀寫操作,Android平台中處理Text這樣的文本MIME的檔案可以使用Java虛擬機器的FileWriter類比較簡單方便。該類位於java.io.FileWriter,提供了多種重寫方法

 FileWriter(File file)  //建立檔案通過File對象

 FileWriter(File file, boolean append) //如果檔案存在,第二個參數設定為false則覆蓋,為true則代表追加方式。

 FileWriter(FileDescriptor fd)  //通過FileDescriptor對象來組建檔案
 
 FileWriter(String filename)  //寫入檔案,參數為完整的路徑和檔案名稱
 
 FileWriter(String filename, boolean append)  //第二個參數通第二種重載方式一樣。

  因為該類從java.io.OutputStreamWriter繼承,具備
void  close()
 
void  flush()
 
String  getEncoding()

void  write(char[] buf, int offset, int count)
 
void  write(String str, int offset, int count)
 
void  write(int oneChar) 
 
 
 還可以使用java.io.Writer
Writer  append(CharSequence csq)
 
Writer  append(CharSequence csq, int start, int end)
 
Writer  append(char c)
 
abstract void  close()
 
abstract void  flush()
 
abstract void  write(char[] buf, int offset, int count)
 
void  write(String str, int offset, int count)
 
void  write(int oneChar)
 
void  write(char[] buf)
 
void  write(String str) 

  對於Text文字檔在Android平台上的讀取,可以使用FileReader類,位於java.io.FileReader,提供了三種重寫方式分別為FileReader(File file) 、FileReader(FileDescriptor fd) 和  FileReader(String filename) 唯一需要注意的是會拋出FileNotFoundException 異常。有關讀取操作有下面幾個父類的方法可以使用

支援 java.io.InputStreamReader 的所有方法
void  close()
String  getEncoding()
int  read(char[] buf, int offset, int length)
int  read()
boolean  ready() 

支援 java.io.Reader 的所有方法
abstract void  close()

void  mark(int readLimit)  //設定一個標記
boolean  markSupported() //判斷是否支援標記mark()或重設reset()方法
abstract int  read(char[] buf, int offset, int count)
int  read(CharBuffer target)
int  read(char[] buf)
int  read()
boolean  ready()
void  reset()  //重設讀取的位置
long  skip(long count) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.