天天看點

java 檔案輸入輸出流總結

與異常動态綁定=》檔案1,在主函數加聲明,2,try包裹,處理在一場中,關閉在finally **************************************************************************************************************************************************** 1,檔案=>輸入流(在函數前抛出異常的聲明)public static void main(String args[])throws IOException

定義 File f=new File("F:/","try.java"); if(!f.exists()) 檔案的建立需要在異常進行中 { try { f.createNewFile(); }catch(IOException e) { } } 常用功能 f.getPath() f.getLength(); f.getName();

System.out.println(f.canRead()); System.out.println(f.canWrite()); System.out.println(f.exists());

繼承關系 Reader=》inputStreamReader=》FileReader stringReader 。 。 Writer=》OutStreamWriter=》FileWriter stringWriter 。 。

2,輸入流(在函數前抛出異常的聲明)public static void main(String args[])throws IOException FileWriter fw=new FileWriter(f,true); try { String s="0"; while(s.compareTo("#")!=0) { s=in.nextLine(); fw.write(s); } }catch(Exception e) { } finally { fw.close();//finally中關閉資源 }*/

3,,輸出流(在函數前抛出異常的聲明)public static void main(String args[])throws IOException FileReader fr=new FileReader(f); int s; try { while(fr.ready()) { s=fr.read(); System.out.print((char)s); } }catch(Exception e) { } finally { fr.close(); } }

關鍵問題=》f.ready() 且讀入的是數字=》需要類型轉換 ****************************************************************************************************************************************************

4位元組流繼承關系 InputStream-》FilterInputStream-》datainputstream/BufferedInputStream outputStream-》FilteroutputStream-》dataoutputstream/BufferedoutputStream

FileInputStream ****************************************************************************************************************************************************ObjectInputstream傳送一個對象

Student s1=new Student("張三", 1, 15, "化學"); Student s2=new Student("李四", 2, 19, "生物"); ObjectOutputStream out=new ObjectOutputStream(目的地); out.writeObject(s1); out.writeObject(s2); out.close(); ObjectInputStream in=new ObjectInputStream(來源地); try { s1=(Student) in.readObject(); s2=(Student) in.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } in.close();

**************************************************************************************************************************************************** 有時候沒有必要傳送整個對象的資訊,隻需要傳送一個對象的某個成員的屬性資訊。 DataOutputStream 輸入 DataoutputStream do=new DataOutIOputStream(目的地); do.writeUTF(s); do.writeInt(1);//存入流 do.flush();//推送至目的地 do.close();//關閉

void writeChar(int v);//将一個char值以2-byte形式寫入到基本輸出流中。先寫入高位元組。 void writeInt(int v);//将一個int值以4-byte值形式寫入到輸出流中先寫高位元組。 void writeUTF(String str);//以機器無關的的方式用UTF-8修改版将一個字元串寫到基本輸出流。該方法先用writeShort寫入兩個位元組表示後面的位元組數。

DataInputStream di=new DataInputStream(來源地); String s=di.readUTF(); int i=di.readInt(); String readUTF();//讀入一個已使用UTF-8修改版格式編碼的字元串 String readLine(); boolean readBoolean; int readInt(); byte readByte(); char readChar();

****************************************************************************************************************************************************

BufferedInputStream

package wenben; import java.io.*; import java.util.*; public class File1 { public static void main(String args[])throws IOException { Scanner in=new Scanner(System.in); File f=new File("F:/","try.java"); if(!f.exists()) { try { f.createNewFile(); }catch(IOException e) { } } System.out.println(f.canRead()); System.out.println(f.canWrite()); System.out.println(f.length()); System.out.println(f.getName()); System.out.println(f.getPath()); FileReader fr=new FileReader(f); int s; try { while(fr.ready()) { s=fr.read(); System.out.print((char)s); } }catch(Exception e) { } finally { fr.close(); } }

}