標籤:需要 tostring 寫檔案 調用 繁體 方法 void stat 上進
Java中的IO流根據流的方向分為 輸入資料流和輸出資料流根據讀取文字的大小 位元組流和字元流 位元組流按位元組讀取 讀取中文時容易亂碼 字元流按字元讀取 通常用於讀取中文根據讀取的方式 節點流和緩衝流
檔案輸入資料流和輸出資料流(FileInputStream FileOutputStream)
FileInputStream fis=null;FileOutputStream fos=null;try { fis=new FileInputStream("D:\\test.txt"); }catch (IOException e) {
e.printStackTrace();
}
如果第二個參數省略,或傳入false 則表示每次寫入時將檔案清空,從檔案頭部開始寫入如果第二個參數傳入true,則表示不清空檔案,在文章末尾處添加
fos=new FileOutputStream("D:\\out.txt",true);
輸出方式
一個位元組一個位元組的讀取位元組int n=-1;while ((n=fis.read())!=-1) { sb.append((char)n);} 將byte數組直接聲明為輸入資料流的長度,一次性讀出所有文字byte[ ] bytes=new byte[fis.available()];fis.read(bytes);sb.append(new String(bytes)); 一次讀取1024個位元組byte[ ] bytes=new byte[1024];int n=-1;while (( n=fis.read(bytes)) !=-1){ sb.append(new String(bytes));} 將字串轉換為byte數組,並通過輸出資料流輸入檔案sb.reverse();fos.write(sb.toString().getBytes());System.out.println(sb);
} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {
}
finally 無論上述代碼是否會出現異常 都會執行的一段代碼通常用於關閉各種資源 傳輸圖片
public static void main(String[] args) {try { FileInputStream fis=new FileInputStream("D:\\test.txt"); FileOutputStream fos=new FileOutputStream("D:\\test.txt"); int n=-1; while ((n=fis.read())!=-1) { fos.write(n); }} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}
}
緩衝輸入資料流和緩衝輸出資料流(BufferedInputStream BufferedOutputStream)
作用
在基本流的基礎上進行封裝,讀取或寫入檔案時,將通過緩衝進行,即,先將內容寫入到緩衝區,緩衝區滿以後但進行讀取或寫入操作可以大大減小檔案的操作次數,提高寫入效率
FileInputStream fis=null; FileOutputStream fos=null; BufferedInputStream bis=null; BufferedOutputStream bos=null; long date1=new Date().getTime(); try {
bis =new BufferedInputStream(new FileInputStream("D:\\test.txt"));//這種寫法,我們成為IO鏈,IO關閉時只需要關閉最外層流,內層流將自動關閉
bos=new BufferedOutputStream(new FileOutputStream("D:\\out.txt",true)); StringBuffer sb=new StringBuffer(); //一個位元組一個位元組的讀取位元組 int n=-1; while ((n=fis.read())!=-1) { sb.append((char)n); } System.out.println(sb); long date2=new Date().getTime(); System.out.println("--------"+(date1-date2)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { // fis.close(); bis.close(); // fos.close(); bos.flush();//在程式最後,重新整理緩衝流,將緩衝流中未滿的內容,寫入到檔案中 bos.close();//調用close方法,將自動重新整理 } catch (IOException e) { e.printStackTrace(); } }
BufferedOutputStream 在關閉前通常調用bos.flush();表示關閉前重新整理緩衝流,將緩衝流中未滿的內容,寫入到檔案中 但是一般close()方法,將自動重新整理緩衝流 資料輸入流與資料輸出資料流(DataInputStream DataOutputStream)
採用二進位對檔案進行讀寫操作與基本流相比,可以直接讀寫Java中的基礎資料型別 (Elementary Data Type) 另外 如果操作的檔案是一個二進位檔案,需要使用DataOutputStream替代FileOutputStream 注意使用DataOutputStream 寫入檔案為二進位檔案 只能使用DataInputStream進行讀取
String name="hhh"; int age=23; double height=134.6; String ads="yantai"; DataInputStream dis=null; DataOutputStream dos=null; try { dos=new DataOutputStream(new FileOutputStream("d:\\test.txt")); dos.writeUTF(name); dos.writeInt(age); dos.writeDouble(height); dos.writeUTF(ads); dis=new DataInputStream(new FileInputStream("d:\\tout.txt")); String uname = dis.readUTF(); int uage = dis.readInt(); double uheight = dis.readDouble(); String uads = dis.readUTF(); System.out.println(uname+"---"+uage+"---"+uheight+"---"+uads); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { dis.close(); dos.flush(); dos.close(); } catch (IOException e) { e.printStackTrace(); } }}
對象輸入資料流和對象輸出資料流(ObjectInputStream ObjectOutputStream)
直接繼承自import java.io.outputStream; 抽象類別與基本流相同,可以直接使用readwrite方法進行讀寫 與DataInputStream相同,可以直接讀寫Java中的基礎資料型別 (Elementary Data Type) readint() writedouble() 可以只用readObject() writeObject()直接對對象進行操作 需要對對象進行對象的序列化與還原序列化 序列化 將程式中的對象 持久化的儲存在檔案中的過程還原序列化 將檔案中的對象 重新讀取到程式中的過程 如果,要對對象進行序列操作,實現可序化介面後,那麼實體類必須實現可序化介面class person implements Serializable{} 注意,當一個實體類 實現可序化介面後 可以添加一個序列化版本號碼ID 會自動產生一個靜態屬性 例如 private static final long serialVersionUID = 2545980081191079819L;添加之後,可以用ID表示序列化與還原序列化時操作的對象 是同一個對象 如果不添加id 當序列化添加一個新對象之後 還原序列化會產生錯誤
public static void main(String[] args) { person zhangsan=new person("張三", 28, 178.5, "煙台"); ObjectInputStream ois =null; ObjectOutputStream oos =null; try { oos=new ObjectOutputStream(new FileOutputStream("d:\\test.txt")); oos.writeObject(zhangsan); ois=new ObjectInputStream(new FileInputStream("d:\\test.txt")); person p=(person)ois.readObject(); System.out.println(p); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { ois.close(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
class person implements Serializable{ private static final long serialVersionUID = 2545980081191079819L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public person(String name) { super(); this.name = name; } }
輸入字元流與輸出字元流(FileWriter FileReader) 字元流在處理資料單元是,以一個字元為單位,而位元組流,以一個字元節為單位 字元的基類reader writer 這兩個是抽象類別FileWriter_FileReaderfilereader filewriter 是直接繼承自抽象類別的兩個字元基本流 filereader filewriter 在讀寫檔案時,只能使用系統預設編碼格式無法指定編碼,如果檔案格式與系統預設格式不一樣,那使用這兩個方法讀寫將造成中文亂碼
public static void main(String[] args) { FileWriter fw=null; FileReader fr=null; try { fw=new FileWriter("D:/io.txt"); String s="1358kkkkk將"; for (int i = 0; i < s.length(); i++) { fw.write(s.charAt(i)); } fw.flush(); fr=new FileReader("D:/io.txt"); int n=0; StringBuffer sb=new StringBuffer(); while ((n=fr.read())!=-1 ){ sb.append((char)n); } System.out.println(sb.toString()); } catch (IOException e) { e.printStackTrace(); }finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
InputStreamReader OutputStreamWriter1.將位元組流轉為字元流,同時支援自訂讀寫的編碼格式2.常見編碼格式 ASCII 美國標準資訊碼 ISO8859-1 歐洲碼 ANSI編碼 分為多種 簡體中文 GB2312 GBK 繁體中文 big-5 Unicode編碼 國際標準碼 相容絕大部分國家的編碼格式 可以分為 UTF-6 UTF-8 UTF-16 字元緩衝輸入資料流與字元緩衝輸出資料流(BufferedWriter BufferedReader)
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/out.txt"),"UTF-8")) ;//表示用UTF-8對字串進行解碼為位元組數組bw.write(sb.toString()+"kkkkkkkkkkkkkkkkkkkkk");
原來的字串 s是UTF-8的編碼格式
String s=sb.toString()+"kkkkkkkkkkkkkkkkkkkkk";s=new String(s.getBytes("UTF-8"),"GBK");//表示將解碼後的位元組數組 重新使用GBK的編碼,組合成字串bw.write(s);
最終,一個UTF-8的字串,經過解碼編碼轉換為GBK格式的字串
Java中的IO流