您好,欢迎访问一九零五行业门户网

IO--转换流,打印流,序列化的实例详解

编码:
把看的懂,变成看不懂的
string str = 中国;
byte[] bytes = str.getbytes();
system.out.println(arrays.tostring(bytes));
解码:
把看不懂的内容,变成能看懂的
string s = new string(bytes);
system.out.println(s);
java.io.outputstreamwriter extends writer
 outputstreamwriter:转换流
 作用:是字符流通向字节流的桥梁,可以指定编码表
继承自父类writer的公共成员方法
  写一个字符,写字符数组,写字符数组的一部分,写字符串,写字符的一部分,刷新,关闭
构造方法:
  outputstreamwriter(outputstream out, string charsetname) 创建使用指定字符集的 outputstreamwriter。
  参数:
  outputstream out:字节输出流(把转换后的字节写入到文件中)
  可以传入fileoutputstream
  string charsetname:编码表名称
  可以传入一个字符串格式的编码表名称,比如gbk,utf-8...,编码表名称不区分大小写,如果不写默认为系统码表
使用步骤:
  1.创建字符输出流fileoutputstream,绑定数据的目的地
  2.创建转换流outputstreamwriter对象,构造方法中传入fileoutputstream和指定的编码表名称
  3.调用outputstreamwriter中写数据的方法,把数据写入到内存缓冲区中
  4.释放资源,并把数据刷新到文件中
 1 public static void main(string[] args) throws ioexception { 2         //write_gbk(); 3         write_utf8(); 4     } 5  6     /* 7      * 使用转换流outputstreamwriter,写utf-8格式的文件 8      */ 9     private static void write_utf8() throws ioexception {10         //1.创建字符输出流fileoutputstream,绑定数据的目的地11         fileoutputstream fos = new fileoutputstream(utf-8.txt);12         //2.创建转换流outputstreamwriter对象,构造方法中传入fileoutputstream和指定的编码表名称13         outputstreamwriter osw = new outputstreamwriter(fos,utf-8);14         //3.调用outputstreamwriter中写数据的方法,把数据写入到内存缓冲区中15         osw.write(你好);16         //4.释放资源,并把数据刷新到文件中17         osw.close();18     }19 20     /*21      * 使用转换流outputstreamwriter,写gbk格式的文件22      */23     private static void write_gbk() throws ioexception {24         //1.创建字符输出流fileoutputstream,绑定数据的目的地25         fileoutputstream fos = new fileoutputstream(gbk.txt);26         //2.创建转换流outputstreamwriter对象,构造方法中传入fileoutputstream和指定的编码表名称27         //outputstreamwriter osw = new outputstreamwriter(fos,gbk);28         outputstreamwriter osw = new outputstreamwriter(fos);29         //3.调用outputstreamwriter中写数据的方法,把数据写入到内存缓冲区中30         osw.write(你好);31         //4.释放资源,并把数据刷新到文件中32         osw.close();33     }
java.io.inputstreamreader extends reader
* inputstreamreader流:是字节流通向字符流的桥梁,可以指定编码表
*
* 继承自父类reader的公共的成员方法
* int read() 读取单个字符。
* int read(char[] cbuf) 将字符读入数组。
* abstract  void close() 关闭该流并释放与之关联的所有资源。
*
* 构造方法:
* inputstreamreader(inputstream in) 创建一个使用默认字符集的 inputstreamreader。
* inputstreamreader(inputstream in, string charsetname) 创建使用指定字符集的 inputstreamreader。
* 参数:
* inputstream in:字节输入流(把文件中保存的字节读取出来)
* 可以传入fileinputstream
* string charsetname:编码表名称
* 可以传入一个字符串格式的编码表名称,比如gbk,utf-8...,编码表名称不区分大小写,如果不写默认为系统码表
*
* 使用步骤:
* 1.创建字节输入流fileinputstream对象,绑定数据源
* 2.创建转换流inputstreamreader对象,构造方法中,传入fileinputstream和指定的编码表名称
* 3.使用inputstreamreader读取数据的方法,读取数据
* 4.释放资源
*
* 注意:构造方法中指定的编码名称,必须和要读取的文件保持一致,否则会出现乱码
 1 public static void main(string[] args) throws ioexception { 2         //read_gbk(); 3         read_utf8(); 4     } 5  6     /* 7      * 使用inputstreamreader读取utf-8格式文件 8      */ 9     private static void read_utf8() throws ioexception {10         //1.创建字节输入流fileinputstream对象,绑定数据源11         fileinputstream fis = new fileinputstream(utf-8.txt);12         //2.创建转换流inputstreamreader对象,构造方法中,传入fileinputstream和指定的编码表名称13         //inputstreamreader isr = new inputstreamreader(fis,gbk);//乱码:浣犲ソ14         inputstreamreader isr = new inputstreamreader(fis,utf-8);//你好15         //3.使用inputstreamreader读取数据的方法,读取数据16         //int read() 读取单个字符。 17         int len = 0;18         while((len = isr.read())!=-1){19             system.out.print((char)len);20         }21         //4.释放资源22         isr.close();23     }24 25     /*26      * 使用inputstreamreader读取gbk格式文件27      */28     private static void read_gbk() throws ioexception {29         //1.创建字节输入流fileinputstream对象,绑定数据源30         fileinputstream fis = new fileinputstream(gbk.txt);31         //2.创建转换流inputstreamreader对象,构造方法中,传入fileinputstream和指定的编码表名称32         //inputstreamreader isr = new inputstreamreader(fis,gbk);33         inputstreamreader isr = new inputstreamreader(fis);//不指定,默认使用gbk34         //3.使用inputstreamreader读取数据的方法,读取数据35         //int read() 读取单个字符。 36         int len = 0;37         while((len = isr.read())!=-1){38             system.out.print((char)len);39         }40         //4.释放资源41         isr.close();42     }
对象的序列化和反序列化
* 对象的序列化:把对象以流的方式写入到文件中保存
* 对象的反序列化:把文件中保存的对象,以流的方式读取出来
对象的反序列化:把文件中保存的对象,以流的方式读取出来
*
* 构造方法:
* objectinputstream(inputstream in) 创建从指定 inputstream 读取的 objectinputstream。
* 参数:
* inputstream in:字节流,可以传入fileinputstream
*
* 读对象的方法:
* object readobject() 从 objectinputstream 读取对象。
*
* 使用步骤:
* 1.创建字节输入流fileinputstream,绑定数据源
* 2.创建反序列化流objectinputstream,构造方法中传入fileinputstream
* 3.使用objectinputstream中的方法readobject,读取文件中保存的对象
* 4.释放资源
* 5.打印对象
*
* 注意:
* 方法readobject,会抛出classnotfoundexception(没有class文件异常)
* 反序列化的前提,必须有对象的class文件存在
对象的序列化:把对象以流的方式写入到文件中保存
* 构造方法:
* objectoutputstream(outputstream out)  创建写入指定 outputstream 的 objectoutputstream。
*  参数:
* outputstream out:字节流,可以传入fileoutputstream
*
* 写对象的方法:
* void writeobject(object obj) 将指定的对象写入 objectoutputstream。
*
* 使用步骤:
* 1.创建对象,并赋值
* 2.创建字节输出流对象fileoutputstream,绑定数据目的地
* 3.创建序列化流objectoutputstream对象,构造方法中传入fileoutputstream
* 4.使用objectoutputstream中的方法writeobject,把对象写入到文件中
* 5.释放资源
*
* 注意:
* 要序列化的类如果没有实现serializable接口,会抛出notserializableexception异常
*/
类通过实现 java.io.serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化
serializable接口:称之为标记型接口
类只有实现了serializable才能序列化和反序列化,不实现就不能
去市场卖肉,肉上有一个蓝色的戳(检测合格),买回去干什么有不同的吃的方式
java.lang.cloneable:标记型接口
类实现了cloneable就能复制,不实现就不能
序列化和反序列的是对象,如果对象中有静态的属性,可以序列化吗?
静态属于类,不属于对象,不能序列化
不管是静态属性和非静态属性都有默认值
age默认值是0,又是静态属性,不能序列化,使用默认值0
瞬态关键字transient:作用,阻止成员变量序列化
打印流:
* 字节打印流:printstream extends outputstream
* 字符打印流:printwriter extends writer
*
* 两个打印流中的方法,完全一致:
* void print(object obj): 输出任意类型的数据,
* void println(object obj): 输出任意类型的数据,自动写入换行操作
*
* 构造方法,就是打印流的输出目的端
* printstream
* 构造方法目的地:接收file类型,接收字符串文件名,接收字节输出流outputstream
* printwriter
* 构造方法目的地:接收file类型,接收字符串文件名,接收字节输出流outputstream,
* 接收字符输出流writer
*
* 注意事项:
* 字节流写入数据的时候,会直接把数据写入到文件中
* 字符流写入数据的时候,会把数据写入到内存缓冲区中,必须刷新或者关闭,才会把数据由缓冲区刷新到文件中
*/''
 1 public static void main(string[] args) throws ioexception { 2         system.out.println(97); 3         method_04(); 4     } 5  6     /* 7      * 字符打印流的自动刷新功能 8      * 自动刷新功能的使用必须满足3个条件: 9      *     1.字符打印流的输出的目的地必须是一个流对象(字符,字节)10      *     2.字符打印流构造方法的参数autoflush必须是true11      *     3.必须使用 println、printf 或 format 方法中的一个才能实现12      *  13      * 包含自动刷新的构造方法:14      *     printwriter(outputstream out, boolean autoflush)  15      *  printwriter(writer out, boolean autoflush)  16      *   17      * 我们可以把字符串的目的地和file类的目的地转换为流,开启自动刷新  18      */19     private static void method_04() throws ioexception {20         printwriter pw = new printwriter(new filewriter(6.txt),true);21         pw.print(不能自动刷新);22         pw.println(会自动把缓冲区中所有的内容刷新到文件中);23     }24 25     /*26      * 打印流,输出目的地,是流对象27      * 可以是字节输出流,也可以是字符输出流28      * outputstream writer29      */30     private static void method_03() throws ioexception {31         fileoutputstream fos = new fileoutputstream(3.txt);32         ////the constructor printstream(filewriter) is undefined33         //printstream ps = new printstream(new filewriter(3.txt));34         printstream ps = new printstream(fos);35         ps.println(字节打印流的输出目的地是一个字节流);36         ps.close();37         38         printwriter pw = new printwriter(new fileoutputstream(4.txt));39         pw.println(字符打印流的输出目的地是一个字节流);40         pw = new printwriter(new filewriter(5.txt));41         pw.println(字符打印流的输出目的地是一个字符流);42         pw.close();43     }44 45     /*46      * 打印流,输出目的,string文件名47      */48     private static void method_02() throws filenotfoundexception {49         printwriter pw = new printwriter(2.txt);50         pw.println(字符打印流,必须的刷新);51         //pw.flush();52         pw.close();53     }54 55     /*56      * 打印流,向file文件对象的数据目的地写入数据57      * 方法 print println 原样输出58      * write方法 走编码表59      */60     private static void method_01() throws filenotfoundexception {61         file file = new file(1.txt);62         printstream ps = new printstream(file);63         //使用继承自父类的write方法写入数据64         ps.write(97);65         //使用自己特有的方法println/print写入数据66         ps.println(97);67         ps.println(true);68         ps.println(hello);69         ps.println(8.8);70         ps.println('中');71         ps.close();72     }
字符打印流的自动刷新功能
自动刷新功能的使用必须满足3个条件:
1.字符打印流的输出的目的地必须是一个流对象(字符,字节)
2.字符打印流构造方法的参数autoflush必须是true
3.必须使用 println、printf 或 format 方法中的一个才能实现
包含自动刷新的构造方法:
printwriter(outputstream out, boolean autoflush)
 printwriter(writer out, boolean autoflush)
我们可以把字符串的目的地和file类的目的地转换为流,开启自动刷新
打印流,输出目的地,是流对象
可以是字节输出流,也可以是字符输出流
outputstream writer
java.io.properties集合 extends hashtable implements map接口
*
* properties集合的特点:
* 1.健和值默认都是string类型
* 2.集合中有自己特有的方法
* object setproperty(string key, string value) 调用 hashtable 的方法 put。
* string getproperty(string key) 用指定的键在此属性列表中搜索属性。  相当于map集合中的get(key k)方法
* set<string> stringpropertynames() 返回此属性列表中的键集.  相当于map集合中的的keyset  
* 3.和io流相结合的方法
* 使用store方法把集合中保存的临时数据,持久化到硬盘的文件中保存
* void store(outputstream out, string comments)  
* void store(writer writer, string comments)
* 使用load方法把硬盘文件中保存的键值对,读取出来,放入到properties集合中  
* void load(inputstream instream)
* void load(reader reader)
 1 public static void main(string[] args) throws ioexception { 2         method_03(); 3     } 4  5     /* 6      * 使用load方法把硬盘文件中保存的键值对,读取出来,放入到properties集合中    7      *     void load(inputstream instream) 8      *     void load(reader reader) 9      *     方法的参数:10      *         inputstream instream:不能读取包含中文的键值对11      *         reader reader:可以读取包含中文的键值对12      *  13      * 使用步骤:14      *     1.创建properties集合15      *     2.创建字节输入流/字符输入对象,绑定数据源16      *     3.使用properties中的方法load,读取文件中保存的键值对,把键值对保存到集合中17      *     4.释放资源18      *     5.遍历properties集合19      *  20      * 注意:21      *         prop.properties文件中使用#号可以注释一行22      *         prop.properties文件中key和value默认就是字符不用使用23      *         prop.properties文件中key和value之间可以使用=连接也可以使用空格24      */25     private static void method_03() throws ioexception {26         //1.创建properties集合27         properties prop = new properties();28         //2.创建字节输入流/字符输入对象,绑定数据源29         filereader fr = new filereader(prop.properties);30         //3.使用properties中的方法load,读取文件中保存的键值对,把键值对保存到集合中31         prop.load(fr);32         //4.释放资源33         fr.close();34         //5.遍历properties集合35         for(string key : prop.stringpropertynames()){36             string value = prop.getproperty(key);37             system.out.println(key+=+value);38         }39     }40 41     /*42      * 使用store方法把集合中保存的临时数据,持久化到硬盘的文件中保存43      *     void store(outputstream out, string comments)   44      *     void store(writer writer, string comments)45      *     方法的参数:46      *         outputstream out:不能操作中文47      *         writer writer:可以操作中文48      *         string comments:注释,保存数据的用途,可以写,不能写中文,默认使用unicode编码49      * 使用步骤:50      *     1.创建properties集合,添加数据51      *     2.创建字节输出流或者字符输出流对象,绑定目的地52      *     3.使用properties集合中的方法store把集合中的数据,写入到文件中53      *     4.释放资源54      */55     private static void method_02() throws ioexception {56         //1.创建properties集合,添加数据57         properties prop = new properties();58         prop.setproperty(a, 1);59         prop.setproperty(b, 2);60         prop.setproperty(中国, 1);61         //2.创建字节输出流或者字符输出流对象,绑定目的地62         filewriter fw = new filewriter(prop.properties);63         //3.使用properties集合中的方法store把集合中的数据,写入到文件中64         prop.store(fw, );65         //4.释放资源66         fw.close();67         68         fileoutputstream fos = new fileoutputstream(prop1.properties);69         prop.store(fos, save date);//写入中文会出现乱码70         fos.close();71     }72 73     /*74      * 使用properties集合中特有的方法,保存数据,遍历集合75      */76     private static void method_01() {77         properties prop = new properties();78         //object setproperty(string key, string value) 调用 hashtable 的方法 put。 79         prop.setproperty(a, 1);80         prop.setproperty(b, 2);81         prop.setproperty(c, 3);82         83         //set<string> stringpropertynames() 返回此属性列表中的键集.  相当于map集合中的的keyset 84         set<string> set = prop.stringpropertynames();85         86         //遍历set集合87         for (string key : set) {88             //string getproperty(string key) 用指定的键在此属性列表中搜索属性。  相当于map集合中的get(key k)方法89             string value = prop.getproperty(key);90             system.out.println(key+...+value);91         }92     }
使用load方法把硬盘文件中保存的键值对,读取出来,放入到properties集合中
* void load(inputstream instream)
* void load(reader reader)
* 方法的参数:
* inputstream instream:不能读取包含中文的键值对
* reader reader:可以读取包含中文的键值对
*
* 使用步骤:
* 1.创建properties集合
* 2.创建字节输入流/字符输入对象,绑定数据源
* 3.使用properties中的方法load,读取文件中保存的键值对,把键值对保存到集合中
* 4.释放资源
* 5.遍历properties集合
*
* 注意:
* prop.properties文件中使用#号可以注释一行
* prop.properties文件中key和value默认就是字符不用使用
* prop.properties文件中key和value之间可以使用=连接也可以使用空格
*/
使用store方法把集合中保存的临时数据,持久化到硬盘的文件中保存
* void store(outputstream out, string comments)  
* void store(writer writer, string comments)
* 方法的参数:
* outputstream out:不能操作中文
* writer writer:可以操作中文
* string comments:注释,保存数据的用途,可以写,不能写中文,默认使用unicode编码
* 使用步骤:
* 1.创建properties集合,添加数据
* 2.创建字节输出流或者字符输出流对象,绑定目的地
* 3.使用properties集合中的方法store把集合中的数据,写入到文件中
* 4.释放资源
使用commons-io中提供的工具类fileutils
* static readfiletostring(file file):读取文件内容,并返回一个string;
* static writestringtofile(file file,string content):将内容content写入到file中;
* static copyfile(file srcfile, file destfile): 文件复制
* static copydirectorytodirectory(file srcdir,file destdir);文件夹复制
*
* 方法都是静态方法,可以通过类名直接使用
* 方法的参数都是file类型
 1 public static void main(string[] args) throws ioexception { 2         //static readfiletostring(file file):读取文件内容,并返回一个string; 3         //string s = fileutils.readfiletostring(new file(prop.properties)); 4         string s = fileutils.readfiletostring(new file(src/cn/itcsat/demo01/demo01filereader.java)); 5         system.out.println(s); 6          7         //static writestringtofile(file file,string content):将内容content写入到file中; 8         fileutils.writestringtofile(new file(fileuitls.txt), fileutils工具类的时候); 9         10         //static copyfile(file srcfile, file destfile): 文件复制11         fileutils.copyfile(new file(c:\\1.jpg), new file(d:\\1.jpg));12         13         //static copydirectorytodirectory(file srcdir,file destdir);文件夹复制14         fileutils.copydirectorytodirectory(new file(c:\\demo),new file(d:));15     }
以上就是io--转换流,打印流,序列化的实例详解的详细内容。
其它类似信息

推荐信息