《零基礎學Java》
-
檔案輸入/輸出流
程式運行期間,大部分資料都被存盤在記憶體中,當程式結束或被關閉時,存盤在記憶體中的資料將會消失,如果要永久保存資料,那么最好的辦法就是把資料保存到磁盤的檔案中,為此,Java提供了檔案輸入/輸出流,即 FilelnputStream類 與 FilcOutputSream類 和 FilcRcader類 與 FileWriter類 ,
-
FilelnputStream類 與 FilcOutputSream類
Java提供了操作磁盤檔案的 FilelnpuSueam類 與 FileOutputStream類 (讀取檔案內容使用的是FilenputStram類;向文作中寫入內容使用的是FileOutputStream類),FilelnputStream類 與 FilcOutputSream類 操作的資料單元是一個位元組,如果檔案中有中文字符則占兩個位元組,
FilelnpuSueam類常用的構造方法:
構造方法 介紹 FilelnpuSueam(String name); 使用指定的檔案名****(name)創建一個FilelnpuSueam物件; FilelnpuSueam(File file); 使用File物件創建FilelnpuSueam物件,(PS:該方法允許把檔案鏈接輸入流) FileOutputStream類常用的構造方法:
構造方法 介紹 FileOutputStream(File file); 使用File物件創建FileOutputStream物件,為檔案寫入資料的檔案輸出流, FileOutputStream(File file , boolean whether); 使用File物件創建FileOutputStream物件,為檔案寫入資料的檔案輸出流,當 whether 為 true 時,位元組寫入檔案末尾處,而不是覆寫, FileOutputStream(String name); 使用指定的檔案名(name)創建一個FilelnpuSueam物件,為檔案寫入資料的檔案輸出流, FileOutputStream(String name , boolean whether); 使用指定的檔案名(name)創建一個FilelnpuSueam物件,為檔案寫入資料的檔案輸出流,當 whether 為 true 時,位元組寫入檔案末尾處,而不是覆寫, FilelnputStream類與FilcOutputSream類 實體:
mport java.io.*; public class Demo2 { public static void main(String[] args) { File file = new File("C:\\12.4.1\\Word.txt"); /** * 輸出流 (FileOutputStream) */ FileOutputStream outputStream =null; try { outputStream=new FileOutputStream(file,true);//輸出流 讀檔案,true:在檔案末尾追加內容; fales:替換檔案內容; String str = "hello word"; byte by[] = str.getBytes();//字串轉換為位元組陣列 try { outputStream.write(by);//將位元組陣列中的資料寫入到檔案當中 } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (outputStream!=null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 輸入流 (FileInputStream) */ FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file);//輸入流 讀檔案 byte by1[] = new byte[128];//創建緩沖區 try { int len = fileInputStream.read(by1);//讀入緩沖區的總位元組數 = 輸入流添加到緩沖區 System.out.println("檔案中的資料為:"+new String(by1,0,len));//去空格 } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } -
FileReader類 與 FileWriter類
FileReader類 與 FileWriter類 對應了 FilelnputStream類 與 FilcOutputSream類;其中讀取檔案內容使用的是FileReader類,向檔案寫入內容使用的是FileWriter類;FileReader類 與 FileWriter類操作的資料單元是一個位元組,如果檔案中有中文字符,就可以使用FileReader類 與 FileWriter類讀取檔案、寫入檔案就可以避免亂碼的產生,
FileReader類 與 FileWriter類 實體:
import java.io.*; public class Demo3 { public static void main(String[] args) { File file = new File("C:\\Word.txt"); /** * 字符輸出流(FileWriter) */ FileWriter fileWriter = null; try { fileWriter=new FileWriter(file,true);// true:在源檔案后追加新內容; fales:覆寫源檔案; String str = "神里綾華,參上!"; fileWriter.write(str);//將字串寫入到文本檔案 } catch (IOException e) { e.printStackTrace(); }finally { if (fileWriter != null){ try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 字符輸入流(FileReader) */ FileReader fileReader = null; try { fileReader=new FileReader(file); char ch[] = new char[1024];//緩沖區 int i;//已讀出的字符數 while ((i=fileReader.read(ch))!=-1){//回圈讀取檔案中的資料,直到所有字符都讀完, System.out.println("檔案中的內容為"+new String(ch,0,i)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fileReader != null){ try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457525.html
標籤:Java
上一篇:SpringCloud Function SpEL注入漏洞分析
下一篇:Java基礎——字符流
