IO流总结 说明: 处理异常用的JDK7之前的方法,推选使用JDK7之后的处理方法
图解 字节流
字符流
对象序列化流
打印流
先复习异常处理
直接上代码 import java.io.*;public class HandleIOException { public static File fileYuan = new File ("src\\A1_MyJava\\基础知识\\IO流\\标准异常处理\\源文件.txt" ); public static File fileNewAppend = new File ("src\\A1_MyJava\\基础知识\\IO流\\标准异常处理" ); public static void main (String[] args) throws IOException { new HandleIOException ().init(); } public void init () throws IOException { ioeJDK6(); ioeJDK7(); ioeJDK9(); } public void ioeJDK6 () { File fileNew = new File (fileNewAppend, "\\IOEjdk6\\" + fileYuan.getName()); BufferedReader br = null ; BufferedWriter bw = null ; try { br = new BufferedReader (new FileReader (fileYuan)); bw = new BufferedWriter (new FileWriter (fileNew)); String str1; while ((str1 = br.readLine()) != null ) { bw.write(str1); bw.newLine(); bw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null ) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (bw != null ) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void ioeJDK7 () { File fileNew = new File (fileNewAppend, "\\IOEjdk7\\" + fileYuan.getName()); try ( BufferedReader br = new BufferedReader (new FileReader (fileYuan)); BufferedWriter bw = new BufferedWriter (new FileWriter (fileNew));) { String str2; while ((str2 = br.readLine()) != null ) { bw.write(str2); bw.newLine(); bw.flush(); } } catch (IOException e) { e.printStackTrace(); } } public void ioeJDK9 () throws IOException { File fileNew = new File (fileNewAppend, "\\IOEjdk9\\" + fileYuan.getName()); BufferedReader br = new BufferedReader (new FileReader (fileYuan)); BufferedWriter bw = new BufferedWriter (new FileWriter (fileNew)); try (br; bw;) { String str3; while ((str3 = br.readLine()) != null ) { bw.write(str3); bw.newLine(); bw.flush(); } } catch (IOException e) { e.printStackTrace(); } } }
File类 创建方法 public boolean createNewFile () {public boolean mkdir () {public boolean mkdirs () {
File 操作方法 public boolean isDirectory () {public boolean isFile () {public boolean exists () {public String getAbsolutePath () {public String getPath () {public String getName () {public String[] list(){public File[] listFiles(){
File删除方法
====================================================================================================
字节流 FileOutputStream 字节输出流 import java.io.FileInputStream;import java.io.IOException;public class TestFileInputStreamReadMethod { public static void main (String[] args) { FileInputStream input = null ; try { input = new FileInputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节流\\字节流读数据\\测试文件1.txt" ); byte [] array = new byte [5 ]; int read1 = input.read(array); System.out.println(read1); System.out.println(new String (array, 0 ,read1)); int read2 = input.read(array); System.out.println(read2); System.out.println(new String (array, 0 ,read2)); }catch (IOException e){ e.printStackTrace(); }finally { if (input != null ){ try { input.close(); }catch (IOException e){ e.printStackTrace(); } } } System.out.println("=============================================" ); try { input = new FileInputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节流\\字节流读数据\\测试文件2.txt" ); byte [] array = new byte [1024 ]; int read; while ((read = input.read(array) ) != -1 ){ System.out.print(new String (array,0 ,read)); } }catch (IOException e){ e.printStackTrace(); }finally { if (input != null ){ try { input.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
import java.io.FileOutputStream;import java.io.IOException;public class ExceptionOutputStream { public static void main (String[] args) { FileOutputStream fos = null ; try { fos = new FileOutputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节流\\字节流写数据\\测试文件3.txt" ); fos.write("问君能有几多愁\n剑圣塔下达不溜" .getBytes()); }catch (IOException e){ e.printStackTrace(); }finally { if (fos != null ){ try { fos.close(); }catch (IOException e){ e.printStackTrace(); } } } FileOutputStream out = null ; try { out = new FileOutputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节流\\字节流写数据\\测试文件3.txt" ); out.write("问君能有几多愁\n剑圣塔下达不溜" .getBytes()); }catch (IOException e){ e.printStackTrace(); }finally { if (out != null ){ try { out.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
====================================================================================================
字节缓冲流 缓冲流输出流 public BufferedOutputStream (OutputStream out) { this (out, 8192 ); } public BufferedOutputStream (OutputStream out, int size) { super (out); if (size <= 0 ) { throw new IllegalArgumentException ("Buffer size <= 0" ); } buf = new byte [size]; }
字符缓冲输入流 import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;public class TestBufferedInputStream { public static void main (String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream (new FileInputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节缓冲流\\测试文件.txt" )); byte [] array = new byte [1024 ]; int len; while ( (len = bis.read(array)) != -1 ){ System.out.print(new String (array,0 ,len)); } bis.close(); } }
字节缓冲流案例 –复制视频 与 不同方法复制所用的时间 package A1_MyJava.基础知识.IO流.字节缓冲流.缓冲流案例;import java.io.*;public class DameBufferedIO_putStream { private static String IN = "D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节缓冲流\\缓冲流案例\\视频文件\\雷神pv.mp4" ; private static String OUT_OR_APPEND = "D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字节缓冲流\\缓冲流案例\\" ; private static String OUT_OR_FILE = "\\雷神pv.mp4" ; public static void main (String[] args) { method1(); method2(); method3(); method4(); method5(); } public static long go () { return System.currentTimeMillis(); } public static long end (long go) { return System.currentTimeMillis() - go; } public static void method1 () { BufferedInputStream bis = null ; BufferedOutputStream bos = null ; long go = go(); try { bis = new BufferedInputStream (new FileInputStream (IN)); bos = new BufferedOutputStream (new FileOutputStream (String.valueOf(new StringBuilder (OUT_OR_APPEND).append("method1" ).append(OUT_OR_FILE)))); int by; while ((by = bis.read()) != -1 ) { bos.write(by); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null ) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null ) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = end(go); System.out.println("method1用时: " + end + "毫秒" ); } public static void method2 () { FileInputStream fis = null ; FileOutputStream fos = null ; long go = go(); try { fis = new FileInputStream (IN); fos = new FileOutputStream (String.valueOf(new StringBuilder (OUT_OR_APPEND).append("method2" ).append(OUT_OR_FILE))); int by; while ((by = fis.read()) != -1 ) { fos.write(by); } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null ) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null ) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = end(go); System.out.println("method1用时: " + end + "毫秒" ); } public static void method3 () { BufferedInputStream bis = null ; BufferedOutputStream bos = null ; long go = go(); try { bis = new BufferedInputStream (new FileInputStream (IN)); bos = new BufferedOutputStream (new FileOutputStream (String.valueOf(new StringBuilder (OUT_OR_APPEND).append("method3" ).append(OUT_OR_FILE)))); byte [] array = new byte [1024 ]; int len; while ((len = bis.read(array)) != -1 ) { bos.write(array, 0 , len); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null ) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null ) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = end(go); System.out.println("method2用时: " + end + "毫秒" ); } public static void method4 () { FileInputStream fis = null ; FileOutputStream fos = null ; long go = go(); try { fis = new FileInputStream (IN); fos = new FileOutputStream (String.valueOf(new StringBuilder (OUT_OR_APPEND).append("method4" ).append(OUT_OR_FILE))); byte [] array = new byte [1024 ]; int by; while ((by = fis.read(array)) != -1 ) { fos.write(array, 0 , by); } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null ) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null ) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = end(go); System.out.println("method1用时: " + end + "毫秒" ); } public static void method5 () { BufferedInputStream bis = null ; BufferedOutputStream bos = null ; long go = go(); try { bis = new BufferedInputStream (new FileInputStream (IN)); bos = new BufferedOutputStream (new FileOutputStream (String.valueOf(new StringBuilder (OUT_OR_APPEND).append("method5" ).append(OUT_OR_FILE)))); byte [] array = new byte [1024 * 32 ]; int len; while ((len = bis.read(array)) != -1 ) { bos.write(array, 0 , len); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null ) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null ) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = end(go); System.out.println("method3用时: " + end + "毫秒" ); } }
====================================================================================================
字符流
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class TestInputStreamReader_Method { public static void main (String[] args) throws IOException { InputStreamReader isr1 = new InputStreamReader ( new FileInputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" )); int by; while ((by = isr1.read()) != -1 ){ System.out.print((char ) by); } isr1.close(); System.out.println("\n\n========================================" ); System.out.println("========================================" ); System.out.println("========================================\n" ); InputStreamReader isr2 = new InputStreamReader ( new FileInputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" )); char [] array = new char [1024 ]; int len; while ((len = isr2.read(array)) != -1 ){ System.out.print(new String (array,0 ,len)); } isr2.close(); System.out.println("\n\n========================================" ); System.out.println("========================================" ); System.out.println("========================================\n" ); InputStreamReader isr3 = new InputStreamReader (new FileInputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\TestOutputStreamWriter_Method.java" )); char [] arrayN = new char [1024 ]; int lenN; while ( (lenN = isr3.read(arrayN)) != -1 ){ System.out.print(new String (arrayN,0 ,lenN)); } isr3.close(); } }
字符输出流 OutputStreamWriter
import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;public class TestOutputStreamWriter_Method { public static void main (String[] args) throws IOException { OutputStreamWriter osw1 = new OutputStreamWriter (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" )); osw1.write(97 ); osw1.flush(); osw1.write("\r\n" ); osw1.close(); OutputStreamWriter osw2 = new OutputStreamWriter (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" ,true )); char [] array = {'a' ,'b' ,'c' ,'中' ,'国' ,'汉' ,'字' ,'\n' }; osw2.write(array); osw2.close(); OutputStreamWriter osw3 = new OutputStreamWriter (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" ,true )); osw3.write(array,3 ,array.length-3 ); osw3.close(); OutputStreamWriter osw4 = new OutputStreamWriter (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" ,true )); String str = "问君能有几多愁,恰似一江春水向东流。\r\n" ; osw4.write(str); osw4.close(); OutputStreamWriter osw5 = new OutputStreamWriter (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_编码解码\\字符流类Method\\文件1.txt" ,true )); osw5.write(str,8 ,str.length()-8 ); osw5.write("========================================" ); osw5.close(); } }
字符流改进与优化 FileReader和 FileWriter package A1_MyJava.基础知识.IO流.字符流.字符流_总结案例;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class DameFileReader_Writer { public static void main (String[] args) throws IOException { FileReader fr = new FileReader ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_总结案例\\DameFileCopy.java" ); FileWriter fw = new FileWriter ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字符流\\字符流_总结案例\\复制到这里\\DameFileCopy_FUBEN.java" ); char [] array = new char [1024 ]; int len; while ((len = fr.read(array))!=-1 ){ fw.write(new String (array,0 ,len)); } fr.close(); fw.close(); } }
编码与解码 字符 编码&&解码 图片
代码 package 基础知识.IO流.字符流.字符流_编码解码;import java.io.*;public class TestIO_putStream_Reader_Writer { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("A1_Java基础SE\\src\\基础知识\\IO流\\字符流\\字符流_编码解码\\文件1.txt" ); OutputStreamWriter osw = new OutputStreamWriter (fos); osw.write("中国汉字" ); osw.write("\r\n" ); osw.write("=======" ); osw.write("\r\n" ); osw.close(); method(); OutputStreamWriter oswNew = null ; InputStreamReader isr = null ; InputStreamReader isrNew = null ; try { oswNew = new OutputStreamWriter ( new FileOutputStream ("A1_Java基础SE\\src\\基础知识\\IO流\\字符流\\字符流_编码解码\\文件2.txt" ,true ) ,"UTF8" ); oswNew.write("\r\n这里可能会出现乱码!" ); isr = new InputStreamReader ( new FileInputStream ("A1_Java基础SE\\src\\基础知识\\IO流\\字符流\\字符流_编码解码\\文件2.txt" )); int by; while ((by = isr.read()) != -1 ){ System.out.print((char ) by); } System.out.println("\n///////////////////////////////////" ); isrNew = new InputStreamReader ( new FileInputStream ("A1_Java基础SE\\src\\基础知识\\IO流\\字符流\\字符流_编码解码\\文件2.txt" ) ,"UTF8" ); int byN; while ((byN = isrNew.read()) != -1 ){ System.out.print((char ) byN); } }catch (IOException e){ e.printStackTrace(); }finally { if (oswNew != null ){ try { oswNew.close(); }catch (IOException e){ e.printStackTrace(); } } if (isr != null ){ try { isr.close(); }catch (IOException e){ e.printStackTrace(); } } if (isrNew != null ){ try { isrNew.close(); }catch (IOException e){ e.printStackTrace(); } } } } public static void method () { OutputStreamWriter osw = null ; try { osw = new OutputStreamWriter (new FileOutputStream ("A1_Java基础SE\\src\\基础知识\\IO流\\字符流\\字符流_编码解码\\文件1.txt" ,true )); osw.write("标准字符流写数据的Code" ); osw.write("\r\n=======\n" ); }catch (IOException e){ e.printStackTrace(); }finally { if (osw != null ){ try { osw.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
字符缓冲流
字符缓冲流特有的Method
代码 public class BufferedRWProMethod { public static void main (String[] args) { newLineMethod(); readLineMethod(); } public static void newLineMethod () { BufferedWriter bw = null ; try { bw = new BufferedWriter (new FileWriter ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字符缓冲流\\字符缓冲流特有方法\\文件.txt" )); bw.write("这就尴尬了" ); bw.newLine(); bw.flush(); bw.write("欸!我换行了!" ); bw.newLine(); bw.flush(); bw.write("卢本伟牛逼,卢本伟没有开挂!" ); bw.newLine(); bw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null ) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void readLineMethod () { BufferedReader br1 = null ; BufferedReader br2 = null ; try { br1 = new BufferedReader (new FileReader ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字符缓冲流\\字符缓冲流特有方法\\文件.txt" )); br2 = new BufferedReader (new FileReader ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\字符缓冲流\\字符缓冲流特有方法\\文件.txt" )); System.out.println(br1.readLine()); System.out.println(br1.readLine()); System.out.println(br1.readLine()); System.out.println(br1.readLine()); System.out.println("==============================" ); String str; while ((str = br2.readLine()) != null ) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } finally { if (br1 != null ) { try { br1.close(); } catch (IOException e) { e.printStackTrace(); } } if (br2 != null ) { try { br2.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
案例 package A1_MyJava.基础知识.IO流.字符缓冲流.字符缓冲流案例;import java.io.*;public class NewJavaCopy { public static void main (String[] args) { BufferedReader br = null ; BufferedWriter bw = null ; try { br = new BufferedReader (new FileReader ("src\\A1_MyJava\\基础知识\\IO流\\字符缓冲流\\字符缓冲流案例\\NewJavaCopy.java" )); bw = new BufferedWriter (new FileWriter ("src\\A1_MyJava\\基础知识\\IO流\\字符缓冲流\\字符缓冲流案例\\复制到这里\\NewJavaCopy.java" )); char [] array = new char [1024 ]; int len; while ((len = br.read(array)) != -1 ) { bw.write(array, 0 , len); System.out.print(new String (array,0 ,len)); } } catch (IOException e) { e.printStackTrace(); } finally { if (br!=null ) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (bw!=null ) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
标准输入输出流 标准输入流
package A1_MyJava.基础知识.IO流.特殊操作流.标准输入输出流.标准输入流;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Scanner;public class StandardInputStream { public static void main (String[] args) throws IOException { new StandardInputStream ().init(); } public void init () throws IOException { Scanner sc = new Scanner (System.in); System.out.print("调用方法:" ); int isMethod = sc.nextInt(); if (isMethod == 1 ) { method1(); } else if (isMethod == 2 ) { method2(); } else if (isMethod == 3 ) { method3(); } else if (isMethod == 4 ) { method4(); } else if (isMethod == 5 ) { method5(); } } public void method1 () throws IOException { System.out.println("已调用方法一" ); InputStream in1 = System.in; int by; while ((by = in1.read()) != -1 ) { System.out.print((char ) by); } in1.close(); } private void method2 () throws IOException { System.out.println("已调用方法二" ); InputStream in = System.in; byte [] array = new byte [1024 ]; int len; while ((len = in.read(array)) != -1 ) { System.out.print(new String (array, 0 , len)); } in.close(); } private void method3 () throws IOException { System.out.println("已调用方法三" ); InputStream in = System.in; InputStreamReader isr = new InputStreamReader (in); char [] array = new char [1024 ]; int len; while ( (len = isr.read(array)) != -1 ){ System.out.print(new String (array,0 ,len)); } isr.close(); } public void method4 () throws IOException { System.out.println("已调用方法四" ); BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); String s = br.readLine(); System.out.println(s); br.close(); } public void method5 () throws IOException { System.out.println("已调用方法五" ); BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); int num = Integer.parseInt(br.readLine()); System.out.println("还需要使用转换工具 :" +num); br.close(); } }
标准输出流
package A1_MyJava.基础知识.IO流.特殊操作流.标准输入输出流.标准输出流;import java.io.PrintStream;public class StandardPrintStream { public static void main (String[] args) { PrintStream ps = System.out; ps.println("字符串" ); ps.println(521 ); ps.println(true ); ps.println('尴' ); ps.print(123 ); ps.print("Go" ); System.out.println("这就是System.out.println()" ); System.out.println(); } }
打印流 字节打印流 PrintStream
import java.io.IOException;import java.io.PrintStream;public class TestPrintStream { public static void main (String[] args) throws IOException { PrintStream ps = new PrintStream ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\字节打印流\\文件.txt" ); ps.write(97 ); ps.println(); ps.print(97 ); ps.println(); ps.println("中文写入" ); ps.close(); } }
字符打印流 PrintWriter
import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class TestPrintWriter { public static void main (String[] args) throws IOException { PrintWriter rw = new PrintWriter ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\字符打印流\\文件.txt" ); rw.write(97 ); rw.flush(); rw.println(); rw.println("尴尬酱万岁!" ); rw.flush(); rw.println("============" ); rw.flush(); rw.close(); PrintWriter rwAuto = new PrintWriter ( new FileWriter ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\字符打印流\\文件.txt" ) ,true ); rwAuto.println("这就尴尬了" ); rwAuto.print("中文" ); rwAuto.flush(); rwAuto.printf("编码" ); rwAuto.println(); rwAuto.format("完成写入" ); rwAuto.close(); } }
打印流案例 import java.io.*;public class PrintWriterCopy { public static void main (String[] args) throws IOException{ method1(); method2(); } private static void method1 () throws IOException { BufferedReader br = new BufferedReader (new FileReader ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\案例打印流复制文件\\PrintWriterCopy.java" )); BufferedWriter bw = new BufferedWriter (new FileWriter ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\案例打印流复制文件\\NewCopy001.txt" )); String str; while ( (str=br.readLine())!= null ){ bw.write(str); bw.newLine(); bw.flush(); } br.close(); bw.close(); } private static void method2 () throws IOException { BufferedReader br = new BufferedReader (new FileReader ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\案例打印流复制文件\\PrintWriterCopy.java" )); PrintWriter pw = new PrintWriter (new FileWriter ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\字节字符打印流\\案例打印流复制文件\\NewCopy002.txt" ) , true ); String str; while ( (str = br.readLine())!= null ){ pw.println(str); } br.close(); pw.close(); } }
对象序列化流 概括
对象序列化流 ObjectOutputStream
import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;public class TestObjectOutputStream { public static void main (String[] args) throws IOException { method2(); } public static void method1 () throws IOException { ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\对象序列反序列化流\\A1_对象序列化流\\对象序列化.txt" )); Student1 stu = new Student1 ("尴尬酱" , 20 ); oos.writeObject(stu); oos.close(); } public static void method2 () throws IOException{ ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ("D:\\源代码\\src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\对象序列反序列化流\\A1_对象序列化流\\对象序列化.txt" )); Student2 stu = new Student2 ("尴尬酱" , 20 ); oos.writeObject(stu); oos.close(); } } public class Student1 { private String name; private int age; public Student1 () { } public Student1 (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } } import java.io.Serializable;public class Student2 implements Serializable { private String name; private int age; public Student2 () { } public Student2 (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } }
import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class TestObjectInputStream { public static void main (String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\对象序列反序列化流\\A1_对象序列化流\\对象序列化.txt" )); Object oStu = ois.readObject(); Student2 stu = (Student2) oStu; ois.close(); System.out.println("姓名: " +stu.getName()+", 年龄: " +stu.getAge()); } }
对象序列化流遇到的问题 与 标准化 问题与解决
比较标准的序列化 上代码 import java.io.*;public class TestObjectStudent { public static void main (String[] args) { read(); } public static void write () { try (ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\对象序列反序列化流\\A3_对象反序列化的问题与标准\\序列化文件.txt" ));) { Student stu = new Student ("尴尬酱" , 20 , 202020 ); oos.writeObject(stu); } catch (IOException e) { e.printStackTrace(); } } public static void read () { try (ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("src\\A1_MyJava\\基础知识\\IO流\\特殊操作流\\对象序列反序列化流\\A3_对象反序列化的问题与标准\\序列化文件.txt" ))) { Object oStu = ois.readObject(); Student stu = (Student) oStu; if (stu.getID() == 0 ){ System.out.println("姓名: " + stu.getName() + ", 年龄: " + stu.getAge() + ", 学号: 无法查看" ); }else { System.out.println("姓名: " + stu.getName() + ", 年龄: " + stu.getAge() + ", 学号: " + stu.getID()); } } catch (Exception e) { e.printStackTrace(); } } } import java.io.Serializable;public class Student implements Serializable { private static final long serialVersionUID = 42L ; private String name; private int age; private transient int ID = 0 ; public Student () { } public Student (String name, int age, int ID) { this .name = name; this .age = age; this .ID = ID; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public int getID () { return ID; } public void setID (int ID) { this .ID = ID; } @Override public String toString () { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", ID=" + ID + '}' ; } }
笔记链接