一.常用的文件操作
1.如何创建文件:
new File(String pathname) //根据路径构建一个File对象
new File(File parent,String child)/根据父目录文件+子路径构建
new File(String parent,String child) //根据父目录+子路径构建
java">public class fileCreat {public static void main(String[] args) {}//方式1:@Testpublic void create01(){String filePath1="D:\\new1.txt";File file = new File(filePath1);try {//执行了createNewfile()才能真正创建文件file.createNewFile();} catch (IOException e) {//throw new RuntimeException(e);e.printStackTrace();}System.out.println("成功");}//方式二。@Testpublic void create02(){//父路径是file对象File parentfile = new File("D:\\");String fileName="new02.txt";File file = new File(parentfile, fileName);try {boolean newFile = file.createNewFile();} catch (IOException e) {throw new RuntimeException(e);}System.out.println("成功");}//方式三。 new File(String parent,String child)@Testpublic void create03(){//父路径是file对象String parentPath = "D:\\";
// String parentPath = "D:/";也可以String fileName="new03.txt";File file = new File(parentPath, fileName);try {boolean newFile = file.createNewFile();} catch (IOException e) {throw new RuntimeException(e);}System.out.println("成功");}}
2.如何获取文件信息
file.getAbsoluteFile());得到文件的绝对路径
file.getParent());得到文件的父级目录
file.length());获取文件大小(字节)
file.exists());判断文件是否存在boolean
file.isFile());是不是文件boolean
file.isDirectory());//是不是目录boolean
java">public class mation_ {public static void main(String[] args) {}public void info(){File file = new File("D://news1.txt");System.out.println(file.getName());System.out.println("绝对路径"+file.getAbsoluteFile());System.out.println("父级目录"+file.getParent());System.out.println("文件大小(字节)"+file.length());System.out.println("文件是否存在"+file.exists());//TrueSystem.out.println("是不是文件"+file.isFile());//TrueSystem.out.println("是不是目录"+file.isDirectory());//Flase}
}
3.目录的操作和文件的删除
file.delete();文件或目录删除
file.mkdir();单级目录的创建
file.mkdirs():多级目录的创建
目录也是文件类型的
java">public class directory_ {public static void main(String[] args) {}@Testpublic void m1(){String filePath="D://new1.txt";File file = new File(filePath);if(file.exists()){file.delete();}else {System.out.println("文件不存在");}}@Testpublic void m2(){String filePath="D://demo";File file = new File(filePath);if(file.exists()){System.out.println("存在");}else {if (file.mkdir()){System.out.println("创建成功");}else {System.out.println("创建失败");}}}@Testpublic void m3(){String filePath="D://demo//a//b";File file = new File(filePath);if(file.exists()){System.out.println("存在");}else {if (file.mkdirs()){System.out.println("创建成功");}else {System.out.println("创建失败");}}}
}
二.IO流原理及流的分类
1.IO流的分类
按操作数据单位不同分为: 字节流(8 bit),字符流(按字符)
按数据流的流向不同分为: 输入流,输出流
按流的角色的不同分为:节点流,处理流/包装流
都是从下面四个抽象基类派生出来的。
抽象基类 | 字节流 | 字符流 |
输入流 | InputStream | Reader |
输出流 | outputStream | Writer |
2.体系图
3.节点流和包装流
节点流可以从一个特定的数据源读写数据,如FileReader、FileWriter。只能处理一种数据。
处理流(也叫包装流)是“连接”在已存在的流(节点流或处理流》之上,为程序提供更为强大的读写功能,也更加灵活,如BufferedReader、BufferedWriter 。可以处理Reader,Wirter的子类类型的数据。
蓝色是节点流,紫色是包装流。包装流可以处理上面的节点流数据。只需传入InputStream或者InputStream子类,就能处理节点流数据。包装类的构造器里也是要传入节点流参数
节点流只能处理对应的数据 。
分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
抽象基类 | InPutStream | OutPutStream | Reader | Writer |
访问文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
访问数组 | ByteArrayInPutStream | ByteOutPutStream | charArrayReader | charArrayWriter |
访问管道 | PipedInPutStream | PipedOutPutStream | PipedReader | PipedWriter |
访问字符串 | StingReader | StingWriter | ||
缓冲流 | BufferedInPutStream | BufferedOutPutStream | BufferedReader | BufferedWriter |
转换流 | InPutStreamReader | OutPutStreamWriter | ||
对象流 | ObjectInPutStream | ObjectOutPutStream | ||
抽象基类 | FilterInPutStream | FilterOutPutStream | FilterReader | FilterWriter |
打印流 | PrintStream | PrintWriter | ||
推回输入流 | PushbackInPutStream | PushbackReader | ||
特殊流 | DataInPutStream | DataOutPutStream |
三.节点流
1.FileInputStream文件输入流(字节)
java">public class FileInputStream01 {public static void main(String[] args) {}//单个字节读取@Testpublic void readFile01() throws IOException {String filePath="D:\\hello.txt";int readDate=0;FileInputStream fileInputStream =null;try {//创建FileInputStream对象,用于读取文件fileInputStream = new FileInputStream(filePath);//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止//如果返回-1,表示读取完毕while ((readDate = fileInputStream.read()) !=-1){System.out.print((char) readDate);//转成char显示,读取的字符是int类型的//读完之后要关闭,释放文件连接}} catch (IOException e) {e.printStackTrace();} finally {//关闭文件,释放资源fileInputStream.close();}}//使用read(byte[]b)读取文件,读取@Testpublic void readFile02() throws IOException {String filePath = "D:\\hello.txt";int readDate = 0;//字节数组byte[] buf=new byte[8];//一次读取8个字节int readLen =0;FileInputStream fileInputStream = null;try {//创建FileInputStream对象,用于读取文件fileInputStream = new FileInputStream(filePath);//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止//如果返回-1,表示读取完毕//如果读取正常,返回实际读取的字节数while ((readLen = fileInputStream.read(buf)) != -1) {System.out.print(new String(buf ,0,readLen));//转成char显示,读取的字符是int类型的//读完之后要关闭,释放文件连接}} catch (IOException e) {e.printStackTrace();} finally {//关闭文件,释放资源fileInputStream.close();}}
}
2.FileOutputStream文件输出流(字节)
java">public class FileOutStream01 {public static void main(String[] args) {}//如果文件不存在,则创建文件@Testpublic void writeFile(){//创建 FileOutputStream 对象String filePath="D://a.txt";FileOutputStream fileOutputStream=null;try {//得到FileOutputStream 对象//1.创建方式是覆盖原来的内容fileOutputStream=new FileOutputStream(filePath);//2.new FileOutputStream(filePath,true);是追加到文件末尾//写入一个字节//fileOutputStream.write('q');//写入字符串String str="helo";fileOutputStream.write(str.getBytes());//getBytes把字符串转为字节数组//写入byte数组的内容,到文件,从0开始到str.lengthfileOutputStream.write(str.getBytes(),0,str.length());} catch (IOException e) {e.printStackTrace();}finally {try {fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}
3.FileReader 文件输入流(字符)
创建:
1) new FileReader(File/String)
2)read:每次读取单个字符,返回该字符,如果到文件未尾返回-1
3)read(char【】): 批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1表示读取完毕
相关API:
1)new String(char[]):将char[]转换成String
2)new String(char[],offlen):将charl的指定部分转换成String
java">public class fileReader {public static void main(String[] args) {}@Testpublic void readFile01(){String filePath = "D://story.txt";FileReader fileReader=null;int data =0;//1.创建 FileReader 对象try {fileReader = new FileReader(filePath);//2.读取while ((data = fileReader.read()) != -1){//read()方法返回的是int类型的System.out.println((char) data);}} catch (IOException e) {e.printStackTrace();}finally {try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}}//字符数组读取文件@Testpublic void readFile02(){String filePath = "D://story.txt";FileReader fileReader=null;int readLen =0;char []buf = new char[8];//1.创建 FileReader 对象try {fileReader = new FileReader(filePath);//2.读取while ((readLen = fileReader.read(buf)) != -1){//read()方法返回的是int类型的,读取到了几个字符System.out.println(new String(buf,0,readLen));//把字符转为字符串}} catch (IOException e) {e.printStackTrace();}finally {try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}}
}
4.FileWriter 文件输出流(字符)
创建:
1) new FileWriter(File/String):相当于流的指针在首端
2) new FileWriter(Fiie/String,true):追加模式,相当于流的指针在尾端
写入:
3) write(int):写入单个字符
4)write(char【】):写入指定数组
5) write(charll,off,,en):写入指定数组的指定部分
6) write (string) : 写入整个字符串
7)write(string,off,len):写入字符串的指定部分
相关API:String类: toCharArray:将String转换成char【】
注意:
FileWriter使用后,必须要关闭(close)或刷新(flush),否则写入不到指定的文件。
java">public class fileEriter {public static void main(String[] args) {}@Testpublic void fileWriter(){FileWriter fileWriter=null;String filePath="D:\\hello.txt";char[] chars={'a','b','c'};try {fileWriter = new FileWriter(filePath);
// 1) write(int):写入单个字符fileWriter.write('H');
// 2) write(char[]):写入指定数组fileWriter.write(chars);
// 3) write(char[],off,Len):写入指定数组的指定部分fileWriter.write("你好啊啊啊".toCharArray(),0,3);
// 4) write (string):写入整个字符串fileWriter.write("你好比较");
// 5) write(string,off,Len):写入字符串的指定部分fileWriter.write("上海阿迪斯",0,2);//上海}catch (IOException e){e.printStackTrace();}}
}
四.包装流
1.包装流设计模式
java">class Test_{public static void main(String[] args) {//读取文件BufferedReader01 bufferedReader01 = new BufferedReader01(new FileReader01());bufferedReader01.readFiles(10);//读取字符串BufferedReader01 bufferedReader011 = new BufferedReader01(new StringRader01());bufferedReader011.readString(5);}
}
public abstract class reader01 {//抽象类,public void readerFile(){}public void readerString(){}
}
class FileReader01 extends reader01{//节点流public void readerFile(){System.out.println("读取文件");}
}
class StringRader01 extends reader01{//节点流public void readerString(){System.out.println("读取字符串");}
}
class BufferedReader01 extends reader01{//这个就是包装流private reader01 r;//属性是reader01public BufferedReader01(reader01 r) {this.r = r;}//public void readFiles(int num){for (int i = 0; i < num; i++) {r.readerFile();}}public void readString(int num){for (int i = 0; i < num; i++) {r.readerString();}}}
2.BufferedReader缓冲输入流(字符)
只需传入Reader或者Reader类,就能处理节点流数据。构造器里传入节点流参数。
BufferedReader 和 BufferedWriter 属于字符流,关闭时处理流,只需要关闭外层流即可。
java">public class bufferReader {public static void main(String[] args) throws IOException {String fP="D://buffer.java";BufferedReader bufferedReader = new BufferedReader(new FileReader(fP));String line;//按行读取//返回null,表示读取完毕while ((line=bufferedReader.readLine()) != null){System.out.println(line);}//只需关闭BufferReader,底层会自动关闭节点流FileReaderbufferedReader.close();}
}
3.BufferedWriter缓冲输入流(字符)
只需传入Writer或者Writer子类,就能处理节点流数据。构造器里传入节点流参数。
public class BufferWriter01 {public static void main(String[] args) throws IOException {String filePath = "D:\\hello.txt";//创建BufferWriter//true表示以覆盖的方式写入BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));bufferedWriter.write("你好");bufferedWriter.write("你好");bufferedWriter.newLine();//换行符bufferedWriter.write("你好");bufferedWriter.write("你好");//关闭外层流即可bufferedWriter.close();}
}
4.BufferedInputStream缓冲输入流(字节)
只需传入InputStream或者InputStream子类,就能处理节点流数据。构造器里传入节点流参数。
5.BufferedOutputStream缓冲输出流(字节)
只需传入OutputStream或者OutputStream子类,就能处理节点流数据。构造器里传入节点流参数。
java">//演示 bufferedOutputStream 和 bufferedInputStream 使用拷贝文件
public class bufferedOutputStream {public static void main(String[] args) {String srcp="D:\\xiao.jpg";String destp="D:\\demo\\qhx.jpg";//创建 BufferedOutputStream 和 BufferedIntputStreamBufferedInputStream bi=null;BufferedOutputStream bo=null;try {bi = new BufferedInputStream(new FileInputStream(srcp));bo = new BufferedOutputStream(new FileOutputStream(destp));//循环的读取文件,并写入到 destFiTePathbyte[] buff = new byte[1024];int readLen = 0;//当返回-1时,就表示文件读取完毕while ((readLen = bi.read(buff)) != -1){bo.write(buff,0,readLen);}} catch (IOException e) {e.printStackTrace();} finally {//关闭外层流try {if (bi != null){bi.close();}if (bo != null){bo.close();}} catch (IOException e) {throw new RuntimeException(e);}}}
}
6.为什么需要对象流?(对象流也是包装流)
将 Dog dog = new Dog(“小黄”,3) 这个 dog对象 保存到 文件中,并且能够从文件恢复.
上面的要求,就是 能够将 基本数据类型 或者 对象 进行 序列化 和 反列化,这就需要对象流。序列化:保存值和数据类型
反序列化:恢复值和数据类型
某个类要实现序列化可以通过下面两个接口:
1.Serializable // 这是一个标记接口,里面没有任何方法,推荐使用这个
2.Externalizable //
7.objectInputStream对象输入流(字节)
序列化
使用ObjectOutputStream 序列化 基本数据类型和 一个 Dog对象(name, age),并
保存到 data.dat 文件中
java">public class objectOutPutStream01 {public static void main(String[] args) throws IOException {//序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存String fp="D:\\data.dat";ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(fp));oo.writeInt(100);// int -> Integer (实现了 Serializable)oo.writeBoolean(true);// boolean -> Boolean (实现了 Serializable)oo.writeChar('a');// char -> Character (实现了 Serializable)oo.writeDouble(9.5);// double -> Double (实现了 Serializable)oo.writeUTF("海贼王");//String//保存一个dog对象oo.writeObject(new Dog("旺财",10));oo.close();System.out.println("数据保存完毕");}
}
class Dog implements Serializable {//把dog类序列化public String name;private int age;public Dog(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;}
}
8.objectOutputStream对象输出流(字节)
反序列化:把dog放到可以引入的位置,然后引入。
java">import com.qhx.Dog;//引入dog类
public class a {public static void main(String[] args) throws IOException, ClassNotFoundException {// 1.创建流对象String fp="D:\\data.dat";ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fp));//2.读取,注意顺序要和保存数据(序列化)的顺序一样System.out.println(ois.readInt());System.out.println(ois.readBoolean());System.out.println(ois.readChar());System.out.println(ois.readDouble());System.out.println(ois.readUTF());//这里会抛异常,抛出去就行了//要把Dog类也拿到到这里Object dog=ois.readObject();//底层Object -> Dog//这里得到的dog编译类型是objectSystem.out.println("运行类型=" + dog.getClass());//如果要使用dog里的方法,要向下转型Dog d=(Dog)dog;d.getAge();//3.关闭ois.close();System.out.println("以反序列化的方式读取(恢复)ok~");}
}
细节:
1.序列化和反序列的读写顺序要一致
2.要求实现序列化或反序列的对象,需要实现Serializable
3.序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
4.序列化对象时,要求里面属性的类型也需要实现序列化接口
5.序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实
现了序列化
9.为什么有转换流?
读取一个文件(字符)时,如果是gdk编码读取成功,改成utf-8可能会读取到乱码。
InputStreamReader(lnputStream, Charset)
转换流:传入一个字节流,和编码方式,把字节流转为字符流
转换流介绍:
1.InputStreamReader:Reader的子类,可以将InputStream(字节流)包装成Reader(字符流)
2.OutputStreamWriter:Writer的子类,实现将OutputStream(字节流)包装成Writer(字符流)
3.当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流。
4.可以在使用时指定编码格式(比如 utf-8,gbk,gb2312,ISO8859-1 等)
10.InputStreamReader 转换流
java">//使用InputStreamReader 转换流解决中文乱码问题
//将字节流 FileInputStream 转成字符流
public class inputStreamReader {public static void main(String[] args) throws IOException {String fp="D://a.txt";//1.创建转换流,传入字节流,指定编码格式InputStreamReader gdk = new InputStreamReader(new FileInputStream(fp), "gbk");//2.创建缓冲流,传入转换流BufferedReader b=new BufferedReader(gdk);//第一二步可以合并为一句String s=b.readLine();//一行一行的读取System.out.println("读取内容"+s);//关闭b.close();}
}
11.OutputStreamWriter转换流
java">//读取文件
public class outputstreamWriter {public static void main(String[] args) throws IOException {//1.转换流(字节流,编码)OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("D:\\a.txt"), "UTF-8");//2.缓冲流(转换流)BufferedWriter b = new BufferedWriter(o);//3.写入b.write("HELLO,你好11啊啊啊啊啊");//4.关闭外层流b.close();}
}
12.打印流
打印流只有输入流,没有输出流
13.printStream字节打印流
java">public class printStream {public static void main(String[] args) throws FileNotFoundException {PrintStream out = System.out;//out属性是System类的一个PrintStream属性out.print("john");//可以使用我们写入方法,写入到标准输出 即显示器,效果一样//out.write("john".getBytes());out.close();//重定向输出设备,到文件System.setOut(new PrintStream("D:\\t1.txt"));//写入内容System.out.println("nihao");//输出到t1.txt中}
}
14.printWriter字符打印流
java">public class printWriter {public static void main(String[] args) throws IOException {//PrintWriter printWriter = new PrintWriter(System.out);//传入OutPutStream的子类,默认打印在控制台PrintWriter printWriter=new PrintWriter(new FileWriter("D:\\a.txt"));printWriter.println("hi,北京你好");//关闭后才能写入进去printWriter.close();}
}
15.properties
用途:配置文件
常见方法:
1.load(): 加载配置文件的键值对到Properties对象
2.list():将数据显示到指定设备
3.getProperty(key),get():根据键获取值,获取到的值是object类型的
4.setProperty(key,value):设置键值对到Properties对象
5.store:将Properties中的键值对存储到配置文件,在idea 中,保存信息到配置文件,如果
含有中文,会存储为unicode码
java">public class properties01 {public static void main(String[] args) throws IOException {//使用 properties 类来读取mysql.properties 文件//1.创建Properties 对象Properties properties = new Properties();//2.加载指定配置文件properties.load(new FileReader("src\\mysql.properties"));//3.把k-v显示控制台properties.list(System.out);//4.根据k获取对应的值String a=properties.getProperty("name");System.out.println(a);}
}
使用 properties 类添加k-v到新文件 mysql2.properties 中
java">public class properties02 {public static void main(String[] args) throws IOException {//把配置文件的内容,存放到另外一个配置文件//1.创建 properties 对象Properties properties = new Properties();//2.设置键值对,(键值对保存在properties对象中)properties.setProperty("charset","utf-8");//properties.setProperty("user","汤姆");properties.setProperty("password","1234");//3.现在把键值对放到 mysql2 配置文件中properties.store(new FileOutputStream("src\\mysql2.properties"),null);//null的位置是对配置文件进行说明,会出现在mysql2的第一行(被注释)System.out.println("放置成功");//4.修改,也是通过setProperties方法properties.setProperty("password","123");//修改}}
五.练习
1.
把一个文件复制到另外一个地方
java">public class fileCopy {public static void main(String[] args) {//把C:\Users\qhx\Pictures\2019922141926_25145.jpg拷贝到D:\\//思路分析//1.创建文件的输入流 ,将文件读入到程序//2.创建文件的输出流,String filePath ="D:\\hello.txt";String destPath = "D:\\";FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream=new FileInputStream(filePath);fileOutputStream=new FileOutputStream(destPath);//定义字节数组byte[] buf=new byte[1024];int readLen =0;while ((readLen =fileInputStream.read(buf)) != -1){//读取到就写出去fileOutputStream.write(buf,0,readLen);//一定要要用这个方法}} catch (IOException e) {e.printStackTrace();}finally {try {if (fileInputStream != null){fileInputStream.close();}if (fileOutputStream != null){fileOutputStream.close();}} catch (IOException e) {throw new RuntimeException(e);}}}
}
2.
使用FileReader从story.txt读取内容
3.
利用BufferedReader和BufferedWriter拷贝文件
java">public class BufferedCopy_ {public static void main(String[] args) {String src="D://hello.txt";String dest="D://aa.txt";//目标文件BufferedReader bufferedReader=null;BufferedWriter bufferedWriter=null;String line;try {bufferedReader = new BufferedReader(new FileReader(src));bufferedWriter = new BufferedWriter(new FileWriter(dest));while ((line=bufferedReader.readLine()) !=null){//读取一行的内容//每读取一行就写入bufferedWriter.write(line);//换行bufferedWriter.newLine();}} catch (IOException e) {e.printStackTrace();}finally {try {//关闭流if (bufferedReader !=null){bufferedReader.close();}if(bufferedWriter != null){bufferedWriter.close();}} catch (IOException e) {throw new RuntimeException(e);}}}
}
4.
(1).在判断D盘下是否有文件夹mytemp ,如果没有就创建mytemp
(2).在D:\\mytemp 目录下,创建文件 hello.txt
(3).如果hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了
java">public class e1 {public static void main(String[] args) throws IOException {String s="D:\\mytemp";File file = new File(s);if(!(file.exists())){System.out.println("文件夹不存在,创建");file.mkdir();}else {System.out.println("文件夹存在");}File file1 = new File(file, "hello.txt");if(!(file1.exists())){if (file1.createNewFile()){System.out.println("hello.txt创建成功");//写入BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file1));bufferedWriter.write("hellooooo");bufferedWriter.close();}else {System.out.println("hello创建失败");}}else {System.out.println("hello.txt存在");}}
}
5.
使用BufferedReader读取一个文本文件,为每行加上行号再连同内容一并输出到屏幕上。
java">public class e2 {public static void main(String[] args) throws IOException {String s="D:\\hello.txt";BufferedReader bufferedReader=null;String line="";int num=0;try {//转换流// FilelnputStream -> InputStreamReader[可以指定字符集]// ->BufferedReaderedInputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(s), "utf-8");bufferedReader=new BufferedReader(inputStreamReader);while ((line=bufferedReader.readLine()) != null){System.out.println(++num + line);}} catch (IOException e) {e.printStackTrace();} finally {bufferedReader.close();}}
}
6.
(1)要编写一个dog.properties : name=tom ,age=5,color=red
(2) 编写Dog 类(name,age,color) 创建一个dog对象,读取dog.properties 用相应的内容完成属性初始化,并输出
(3) 将创建的Dog 对象 ,序列化到 文件 dog.dat 文件
java">public class e3 {public static void main(String[] args) throws IOException {Properties properties = new Properties();properties.store(new FileOutputStream("src\\dog.properties"),null);String name = properties.get("name")+"";//properties.get返回值是obj类型的int age = Integer.parseInt(properties.get("age")+"");//s->iString color = properties.get("color")+"";Dog dog = new Dog(name, age, color);System.out.println(dog.toString());//序列化String f="D:\\dog.dat";ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(f));o.writeObject(dog);o.close();}//编写一个方法,反序列化@Testpublic void m1() throws IOException, ClassNotFoundException {String f1="D:\\dog.dat";ObjectInputStream oo=new ObjectInputStream(new FileInputStream(f1));Dog d=(Dog) oo.readObject();oo.close();}
}class Dog implements Serializable {private String name;private int age;private String color;public Dog(String name, int age, String color) {this.name = name;this.age = age;this.color = color;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", color='" + color + '\'' +'}';}
}