JAVA学习笔记10——属性集,缓冲流,转换流,序列化,打印流

    技术2022-07-12  56

    属性集

    1.Properties类 Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载 Properties集合是一个唯一和IO流相结合的集合 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用 属性列表中每个键及其对应的值都是一个字符串。 Properties集合是一个双列集合,key和value默认都是字符串 构造方法: public Properties():创建一个空的属性列表 基本存储方法: public Object setProperty(String key,String value):保存一对属性 public String getProperty(String key):使用此属性列表中指定的键搜索属性值 public Set stringPropertyNames():所有键的名称的集合

    public class Demo01 { public static void main(String[] args) { show01(); } //使用Properties 集合存储数据,遍历取出Properties集合中的数据 private static void show01() { //创建Properties集合对象 Properties prop=new Properties(); //使用setProperty往集合中添加数据 prop.setProperty("Kobe","24"); prop.setProperty("James","23"); prop.setProperty("Curry","30"); //使用stringPropertyNames把Properties集合中的键取出存储到一个set集合中 Set<String> set=prop.stringPropertyNames(); //遍历set集合,取出Properties集合中的每一个键 for (String s : set) { //使用getProperty方法通过s获取value String value=prop.getProperty(s); System.out.println(s+":"+value); } } }

    store方法 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储

    public class Demo02 { public static void main(String[] args) throws IOException { show01(); } //使用Properties集合中的方法store,把集合中的临时数据,持久化写入硬盘 private static void show01() throws IOException { //创建Properties集合对象,添加数据 Properties prop=new Properties(); prop.setProperty("Kobe","24"); prop.setProperty("James","23"); prop.setProperty("Curry","30"); //创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 FileWriter fw=new FileWriter("F:\\JAVA\\File\\text1.txt"); //使用Properties中的方法store,把集合中的临时数据,永久写入到硬盘中存储 prop.store(fw,"save data"); //释放资源 fw.close(); } }

    load方法 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用 存储键值对的文件中,键与值磨人的连接符号可以使用=,空格(其他符号) 存储键值对的文件中,可以使用#进行注释,注释内容不会被读取 存储键值对的文件中,键与值默认都是字符串

    public class Demo03 { public static void main(String[] args) throws IOException { show01(); } private static void show01() throws IOException { //创建Properties集合对象 Properties prop=new Properties(); //使用Properties集合对象中的load读取保存键值对的文件 prop.load(new FileReader("F:\\JAVA\\File\\text1.txt")); //遍历Properties Set<String> set=prop.stringPropertyNames(); for (String s : set) { String value = prop.getProperty(s); System.out.println(s+":"+value); } } }

    缓冲流

    1.字节缓冲流 public BufferedInputStream(InputStream in):创建一个新的缓冲输入流,给基本的字节输入流增加一个缓冲区(数组)提高基本的字节输入流的读取数据 public BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流 字节缓冲输出流: public BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流,可以传 递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率

    public class Demo04 { public static void main(String[] args) throws IOException { //创建FileOutputStream对象,构造方法中绑定要输出的目的地 FileOutputStream fos=new FileOutputStream("F:\\JAVA\\File\\text1.txt"); //创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率 BufferedOutputStream bos=new BufferedOutputStream(fos); //使用BufferedOutputStream对象中的write,把数据写入到内部缓冲区中 bos.write("写入数据到缓冲区".getBytes()); //使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据刷新到文件中 bos.flush(); //释放资源 bos.close(); } }

    字节缓冲输入流: public BufferedInputStream(InputStream in):创建一个新的缓冲输入流,可以传 递FileInputStream,缓冲流会给FileInputStream增加一个缓冲区,提高FileInputStream的写入效率

    public class Demo05 { public static void main(String[] args) throws IOException { //创建FileInputStream对象,构造方法中绑定要读取的数据 FileInputStream fis=new FileInputStream("F:\\JAVA\\File\\text1.txt"); //创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream读取效率 BufferedInputStream bis=new BufferedInputStream(fis); //使用BufferedOutputStream对象中的read,读取文件 int len=0; while((len=bis.read())!=-1){ System.out.println(len); } //释放资源 bis.close(); } }

    复制文件效率比较:

    public class Demo06 { public static void main(String[] args) throws IOException { long start=System.currentTimeMillis(); //创建字节缓冲输入流对象,构造方法中传递字节输入流 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\\JAVA\\File\\picture.jpg")); //创建字节缓冲输出流对象,构造方法中传递字节输出流 BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\\JAVA\\File\\FileCopy\\picture.jpg")); //使用字节缓冲输入流对象中的方法read读取文件 //使用字节缓冲输出流对象中的方法write把读取的数据写入到内部缓冲区中 int len=0; while((len=bis.read())!=-1) { bos.write(len); } //释放资源 bos.close(); bis.close(); long end=System.currentTimeMillis(); System.out.println("耗时"+(end-start)+"毫秒"); } }

    2.字符缓冲流 字符缓冲输出流 构造方法: BufferedWriter(Writer out):创建一个使用默认大小输出缓冲区的缓冲字符输出流 Writer out:字符输出流 我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率 特有成员方法: void newLine():写入一个行分隔符,会根据不同的操作系统获取不同的行分隔符

    public class Demo07 { public static void main(String[] args) throws IOException { //创建一个字符缓冲输出流,构造方法中传递字符输出流 BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\JAVA\\File\\text1.txt")); //调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中 for(int i=0;i<10;i++){ bw.write("Kobe"); bw.newLine(); } //调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据刷新到文件中 bw.flush(); //释放资源 bw.close(); } }

    字符缓冲输入流 构造方法: BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符输入流 Reader in:字符输入流,我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率 特有的成员方法: String readLine():读取一个文本行。读取一行数据

    public class Demo08 { public static void main(String[] args) throws IOException { //创建字符缓冲输入流对象,构造方法中传递字符输入流 BufferedReader br=new BufferedReader(new FileReader("F:\\JAVA\\File\\text1.txt")); //使用字符缓冲输入流对象中的方法read/readLine读取文本 String line; while((line=br.readLine())!=null){ System.out.println(line); } br.close(); } }

    转换流

    1.字符编码与字符集 字符编码:一套自然语言的字符与二进制数之间的对应规则 字符集:编码表。一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等 2.InputStreamReader类 InputStreamReader(InputStream in):创建一个使用默认字符集的字符流 InputStreamReader(InputStream in,String charsetName):创建一个指定字符集的字符流

    public class Demo02 { public static void main(String[] args) throws IOException { //创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称 InputStreamReader isr=new InputStreamReader(new FileInputStream("F:\\JAVA\\File\\text1.txt"),"UTF-8"); //创建InputStreamReader对象中的方法read读取文件 int len; while((len=isr.read())!=-1){ System.out.println((char) len); } //释放资源 isr.close(); } }

    3.OutputStreamWriter类 OutputStreamWriter(OutputStream out):创建一个使用默认字符集编码的字节输出流 OutputStreamWriter(OutputStream out,String charsetName):创建使用指定字符集的字节输出流

    public class Demo01 { public static void main(String[] args) throws IOException { //创建一个OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称 OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("F:\\JAVA\\File\\text1.txt"),"utf-8"); //使用OutputStreamWriter对象中的write,把字符转换为字节存储缓冲区中 osw.write("你好"); //使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中 osw.flush(); //释放资源 osw.close(); } }

    序列化

    序列化:把对象以流的方式写入到文件中保存,叫写对象,也叫序列化 反序列化:把文件中保存的对象,以流的方式读取出来,叫做读对象,也叫反序列化 序列化与反序列化的时候,会抛出NotSerializableException没有序列化异常 类通过实现接口以启用序列化功能,未实现此接口的类将五福使其任何状态序列化或反序列化 要进行序列化和反序列化的类必须实现Serializable接口,就会给类添加一个标记 当我们进行序列化和反序列化的时候,就会检测这个类是否有这个标记 如果有就可以序列化与反序列化,如果无就会抛出异常 1.ObjectOutputStream类 构造方法: public ObjectOutputStream(OutputStream out):创建一个指定的OutputStream的ObjectOutputStream(字节输出流) 特有的成员方法: void writeObject(Object obj):将指定的对象写入ObjectOutputStream

    public class Demo03 { public static void main(String[] args) throws IOException { //创建ObjectOutputStream对象,构造方法中传递字节输出流 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("F:\\JAVA\\File\\Person.txt")); //使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中 oos.writeObject(new Person("Kobe",24)); //释放资源 oos.close(); } }

    2.ObjectInputStream类 构造方法: public ObjectInputStream(InputStream in):创建从指定InputStream读取的ObjectInputStream 特有的成员方法: Object readObject():从ObjectInputStream读取对象

    public class Demo04 { public static void main(String[] args) throws IOException, ClassNotFoundException { //使用ObjectInputStream对象,构造方法中传递字节输入流 ObjectInputStream ois=new ObjectInputStream(new FileInputStream("F:\\JAVA\\File\\Person.txt")); //使用ObjectInputStream对象中的方法readObject读取保存对象的文件 Object o=ois.readObject(); //释放资源 ois.close(); //使用读取出来的对象 System.out.println(o); } }

    transient:瞬态关键字,修饰成员变量不能序列化

    打印流

    PrintStream:为其他输出流添加了功能,使他们能够方便地打印各种数据值表示形式 特点: 1.只负责数据的输出,不负责数据的读取 2.与其他输出流不同,PrintStream不会抛出IOException 3.有特有的方法 构造方法: PrintStream(File file):输出的目的地是一个文件 PrintStream(OutputStream out):输出的目的地是一个字节输出流 PrintStream(String fileName):输出的目的地是一个文件路径

    Processed: 0.012, SQL: 9