java1.7與Android中的檔案I/O操作(草稿)

來源:互聯網
上載者:User

標籤:檔案io

說明:檔案I/O的關鍵類比較多,這裡總結一下。

參考:《Java 7 入門經典》

1、Java輸入資料流

圖1,java常用輸入輸出資料流的結構圖(第一版)

 

 

1)標準流

標準流

類型

方法

System.in

InputStream

 

System.out

PrintStream

例如:printf(“b=%2$-15.2f”,a,b)

System.err


2)位元組型

典型方式-1:從檔案中讀取

 File file = ...   InputStream in = null;   try {     in = new BufferedInputStream(new FileInputStream(file));     ...    finally {     if (in != null) {       in.close();     }   } }

典型方式-2:從檔案中讀取(java1.7)

Files.newInputStream(Path file)Path file = Path.get(“D:/cao.java”)   InputStream in = null;   try {    in = new BufferedIntputStream(Files.newInputStream(file));     ...    finally {     if (out != null) {       out.close();     }   } }
典型方式-3從控制台讀取
in = new BufferedInputStream(System.in);

3)字元型

典型方式-1:從檔案中讀取(***不太好***)

BufferedReader buf=new BufferedReader(new FileReader(String file))//不能編碼BufferedReader buf = new BufferedReader(new FileReader("file.java"));

典型方式-2:從檔案中讀取(***較好***)

BufferedReader buf=new BufferedReader(new InputStreamReader(new FileInputStream(String file), String charsetName))  //可以編碼InputStreamReader(InputStream in, String charsetName) File file = ... ...BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(String file), “utf-8”);

典型方式-3:從檔案中讀取(***java1.7最佳**)

InputStream newInputStream(Path path,OpenOption... options)Path file=Paths.get();…BufferedInputStream in=new BufferedInputStream(Files.newInputStream(file, Charset.forName(“UTF-8”)))
4)通道輸入資料流(-- 待完成 --)2、Java輸出資料流
1)位元組型

典型方式-1:開啟檔案的輸出資料流

FileOutputStream (File file,boolean append),append為true追加模式,沒有則建立

 File file = ...   OutputStream out = null;   try {    out = new BufferedOutputStream(new FileOutputStream(file));     ...    finally {     if (out != null) {       out.close();     }   } }

典型方式-2:開啟檔案的輸出資料流(java1.7)

Files.newOutputStream(Pathfile,CREATE,APPEND),沒有則建立,追加模式

Path file = Path.get(“D:/caoyanfeng.java”)   OutputStream out = null;   try {    out = new BufferedOutputStream(Files.newOutputStream(file));     ...    finally {     if (out != null) {       out.close();     }   } }
2)字元型

典型方式-1:開啟檔案的輸出資料流(***不好的方式***)

FileWriter(File file,boolean append),append為true追加模式,沒有則建立。不能指定編碼方式。

File file = ...   Writer writer = null;   try {    writer = new BufferedWriter(new FileWriter(file));     ...    finally {     if (writer != null) {       writer.close();     }   } }

 典型方式-2:開啟檔案的輸出資料流(***較好方式***)

OutputStreamWriter(new FileOutputStream(File file,  booleanappend),"UTF-8") , append為true追加模式,沒有則建立,utf-8編碼

File file = ...  Writer writer = null;   try {    writer = new OutputStreamWriter(new FileOutputStream(file),”UTF-8”);     ...   writer.write("曹豔豐");   writer.flush;    finally {     if (writer != null) {       writer.close();     }   } }

典型方式-3:開啟檔案的輸出資料流(***最佳方式***)

Files.newBufferedWriter(Path file,Charset.forName(“UTF-8”), CREATE,APPEND),沒有則建立,追加模式,utf-8編碼

Path file = Path.get(“D:/cao.java”)  Writer writer = null;   try {    writer = new BufferedWriter(Files. newBufferedWriter (Path file,Charset.forName(“UTF-8”), CREATE,APPEND));     ...    finally {     if (writer != null) {       writer.close();     }   } }
3)通道輸出資料流(-- 待完成 --)
3、Android輸入資料流

Android 的輸入輸出資料流基本和java相同,只是路徑需要使用手機路徑。另外,context提供了操作程式路徑下的兩個位元組流。

FileInputStream openFileInput (String name)

FileOutputStream openFileOutput (String name,int mode)

1)位元組型

獲得檔案的FileInputStream,然後進行封裝

BufferedInputStream  in = new BufferedInputStream(new FileInputStream(File file));

典型例子-1:操作本程式data檔案夾的IO

Android的context方法

FileInputStream openFileInput (String name)

典型例子-2:指定路徑下的IO(同java)

File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");BufferedInputStream  in = new BufferedInputStream(new FileInputStream(File file));

2)字元型3、 Android輸出資料流1)位元組型

獲得檔案的FileOutputStream,然後進行封裝

再將FileOutputStreamfos進行封裝進PrintStream 或BufferedOutputStream

BufferedOutputStream out=new  BufferedOutputStream(openFileInput (String name)或

PrintStream out=new BufferedOutputStream(openFileInput (String name))

典型例子-1:操作本程式data檔案夾的IO

 FileOutputStream openFileOutput (String name, int mode)即 FileOutputStream fos= openFileInput (String name)

典型例子-2:操作指定檔案夾的下的IO

FileOutputStream(File file, boolean append),與java位元組流相同File dir = Environment.getDataDirectory();//得到data目錄   File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");   FileOutputStream out=new FileOutputStream(file, true);  
2)字元型

典型例子-1:開啟檔案的輸出資料流(***同java,不好的方式***)

File dir = Environment.getDataDirectory();//得到data目錄 File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");   FileWriter fw=new FileWriter(file,true);   

到這裡可以直接寫入檔案,也可以進行再次封裝

BufferedWriter bw=new BufferedWriter(new FileWriter(file,true));

典型方式-2:開啟檔案的輸出資料流(***同java,較好方式***)

File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");OutputStreamWriter(new FileOutputStream(File file,  boolean append),"UTF-8") // append為true追加模式,沒有則建立,utf-8編碼

























java1.7與Android中的檔案I/O操作(草稿)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.