-
一、被序列化的對象要實現序列化接口: public class Student implements Serializable{ 二.對象的序列化: public static void main(String[] args) throws Exception{ //序列化后存到這個文件里 String file = "demo/obj.dat"; //1.對象的序列化 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(file)); Student stu = new Student("10001", "張三", 20); oos.writeObject(stu); oos.flush(); oos.close();查看全部
-
3.對象的序列化,反序列化 1)對象序列化,就是將Object轉換成byte序列,反之叫對象的反序列化 2)序列化流(ObjectOutputStream),是過濾流----writeObject 反序列化流(ObjectInputStream)---readObject 3)序列化接口(Serializable) 對象必須實現序列化接口 ,才能進行序列化,否則將出現異常 這個接口,沒有任何方法,只是一個標準查看全部
-
public static void main(String[] args) throws IOException{ //對文件進行讀寫操作 BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream("e:\\javaio\\imooc.txt"))); PrintWriter pw = new PrintWriter("e:\\javaio\\imooc4.txt"); String line ; while((line = br.readLine())!=null){ System.out.println(line);//一次讀一行,并不能識別換行 pw.println(line); pw.flush(); } br.close(); pw.close(); }查看全部
-
字符流的過濾器 BufferedReader ---->readLine 一次讀一行 BufferedWriter/PrintWriter ---->寫一行 public static void main(String[] args) throws IOException{ //對文件進行讀寫操作 BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream("e:\\javaio\\imooc.txt"))); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("e:\\javaio\\imooc3.txt"))); String line ; while((line = br.readLine())!=null){ System.out.println(line);//一次讀一行,并不能識別換行 bw.write(line); //單獨寫出換行操作 bw.newLine();//換行操作 bw.flush(); ; } br.close(); bw.close(); }查看全部
-
FileReader/FileWriter public static void main(String[] args) throws IOException{ FileReader fr = new FileReader("e:\\javaio\\imooc.txt");//和FileInputStream的區(qū)別就是無法制定讀取的編碼類型,可能會有亂碼 FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt"); //FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt",true); char[] buffer = new char[2056]; int c ; while((c = fr.read(buffer,0,buffer.length))!=-1){ fw.write(buffer,0,c); fw.flush(); } fr.close(); fw.close(); }查看全部
-
2.字符流 1) 編碼問題 2)認識文本和文本文件 java的文本(char)是16位無符號整數,是字符的unicode編碼(雙字節(jié)編碼) 文件是byte byte byte ...的數據序列 文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化為byte的存儲結果 3)字符流(Reader Writer)---->操作的是文本文本文件 字符的處理,一次處理一個字符 字符的底層任然是基本的字節(jié)序列 字符流的基本實現 InputStreamReader 完成byte流解析為char流,按照編碼解析 OutputStreamWriter 提供char流到byte流,按照編碼處理查看全部
-
public static void main(String[] args)throws IOException { FileInputStream in = new FileInputStream("e:\\javaio\\imoocutf8.txt"); InputStreamReader isr = new InputStreamReader(in,"utf-8");//默認項目的編碼,操作的時候,要寫文件本身的編碼格式 FileOutputStream out = new FileOutputStream("e:\\javaio\\imoocutf81.txt"); OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8"); /*int c ; while((c = isr.read())!=-1){ System.out.print((char)c); }*/ char[] buffer = new char[8*1024]; int c; /*批量讀取,放入buffer這個字符數組,從第0個位置開始放置,最多放buffer.length個 返回的是讀到的字符的個數 */ while(( c = isr.read(buffer,0,buffer.length))!=-1){ String s = new String(buffer,0,c); System.out.print(s); osw.write(buffer,0,c); osw.flush(); } isr.close(); osw.close(); }查看全部
-
2.字符流 1) 編碼問題 2)認識文本和文本文件 java的文本(char)是16位無符號整數,是字符的unicode編碼(雙字節(jié)編碼) 文件是byte byte byte ...的數據序列 文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化為byte的存儲結果 3)字符流(Reader Writer)---->操作的是文本文本文件查看全部
-
java.io.File類用于表示文件(目錄),只用與表示文件(目錄)的信息(名稱,大?。荒苡糜谖募热菰L問 可以創(chuàng)建目錄和文件,以及刪除等,獲取路徑、文件名,父目錄查看全部
-
Java I/O 編碼: gbk:英文字母占一個字節(jié),中文兩個自己 utf-8: 英文站一個自己額,中文占三個字節(jié) java是雙字節(jié)編碼 utf-16be utf-16be:英文占用兩個字節(jié),中文占用兩個字節(jié) 當字節(jié)是某種編碼,如果要反編碼為字符需要用對應的編碼方式。 問本文件就是字節(jié)序列,可以是任意編碼的字節(jié)序列 如果在中文機器上直接創(chuàng)建文本文件,那么該文件只認識ansi編碼 聯通 聯 一種巧合,他們正好符合了utf-8的編碼規(guī)則。查看全部
-
8)BufferedInputStream&BufferedOutputStream 這兩個流類位IO提供了帶緩沖區(qū)的操作,一般打開文件進行寫入 或讀取操作時,都會加上緩沖,這種流模式提高了IO的性能 從應用程序中把輸入放入文件,相當于將一缸水倒入到另一個缸中: FileOutputStream--->write()方法相當于一滴一滴地把水“轉移”過去 DataOutputStream-->writeXxx()方法會方便一些,相當于一瓢一瓢把水“轉移”過去 BufferedOutputStream--->write方法更方便,相當于一飄一瓢先放入桶中,再從桶中倒入到另一個缸中,性能提高了 /** * 進行文件的拷貝,利用帶緩沖的字節(jié)流 * @param srcFile * @param destFile * @throws IOException */ public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{ if(!srcFile.exists()){ throw new IllegalArgumentException("文件:"+srcFile+"不存在"); } if(!srcFile.isFile()){ throw new IllegalArgumentException(srcFile+"不是文件"); } BufferedInputStream bis = new BufferedInputStream( new FileInputStream(srcFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); int c ; while((c = bis.read())!=-1){ bos.write(c); bos.flush();//刷新緩沖區(qū) } bis.close(); bos.close(); }查看全部
-
/** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub String file = "demo/dos.dat"; IOUtil.printHex(file); DataInputStream dis = new DataInputStream( new FileInputStream(file)); int i = dis.readInt(); System.out.println(i); i = dis.readInt(); System.out.println(i); long l = dis.readLong(); System.out.println(l); double d = dis.readDouble(); System.out.println(d); String s = dis.readUTF(); System.out.println(s); dis.close(); }查看全部
-
7)DataOutputStream/DataInputStream 對"流"功能的擴展,可以更加方面的讀取int,long,字符等類型數據 DataOutputStream writeInt()/writeDouble()/writeUTF()h public class DosDemo { public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; DataOutputStream dos = new DataOutputStream( new FileOutputStream(file)); dos.writeInt(10); dos.writeInt(-10); dos.writeLong(10l); dos.writeDouble(10.5); //采用utf-8編碼寫出 dos.writeUTF("中國"); //采用utf-16be編碼寫出 dos.writeChars("中國"); dos.close(); IOUtil.printHex(file); }查看全部
-
/** * 文件拷貝,字節(jié)批量讀取 * @param srcFile * @param destFile * @throws IOException * * JAVA的throw和throws區(qū)別: * http://zhidao.baidu.com/link?url=cOoyefusSFJRvXFuNK3vAVS_mGcE3jgWSy8CiwZk5y-N8Fa-m_cwRrNVEneXKkwMOTYHz8MIIS13gAz91Y4vZ_ */ public static void copyFile(File srcFile,File destFile)throws IOException{ if(!srcFile.exists()){ throw new IllegalArgumentException("文件:"+srcFile+"不存在"); } if(!srcFile.isFile()){ throw new IllegalArgumentException(srcFile+"不是文件"); } FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); byte[] buf = new byte[8*1024]; int b ; while((b = in.read(buf,0,buf.length))!=-1){ //講buf數組裡的內容讀入到該輸出流中(out) out.write(buf,0,b); out.flush();//最好加上 } in.close(); out.close(); }查看全部
-
/** * @param args */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //如果該文件不存在,則直接創(chuàng)建,如果存在,刪除后創(chuàng)建 FileOutputStream out = new FileOutputStream("demo/out.dat"); out.write('A');//寫出了'A'的低八位 out.write('B');//寫出了'B'的低八位 int a = 10;//write只能寫八位,那么寫一個int需要些4次每次8位 out.write(a >>> 24); out.write(a >>> 16); out.write(a >>> 8); out.write(a); byte[] gbk = "中國".getBytes("gbk"); out.write(gbk); out.close(); IOUtil.printHex("demo/out.dat"); }查看全部
舉報
0/150
提交
取消