Java中的IO流
阿新 • • 發佈:2018-04-22
dex pen 標準 args all uname 文件清空 bsp 如果 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中的基本數據類型 另外 如果操作的文件是一個二進制文件,需要使用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中的基本數據類型 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_FileReader filereader 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 OutputStreamWriter 1.將字節流轉為字符流,同時支持自定義讀寫的編碼格式 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流