黑马程序员__字符流_序列流_内存流_对象操作流_打印流_标准输出流_数据输入输出流及Properties



------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一.字符流

1.字符流是什么

       字符流是可以直接读写字符的IO流

       字符流读取字符, 就要先读取到字节数据, 然后转为字符.如果要写出     字符, 需要把字符转为字节再写出.   

    2.FileReader, FileWriter

       FileReader类的read()方法可以按照字符大小读取

       FileWriter类的write()方法可以自动把字符转为字节写出

[java]  view plain copy
  1. import java.io.FileReader;  
  2. import java.io.IOException;  
  3.   
  4. public class Demo1_FileReader {  
  5.   
  6.     /** 
  7.      * @param args 
  8.      * @throws IOException  
  9.      */  
  10.     public static void main(String[] args) throws IOException {  
  11.         //demo1();  
  12.         FileReader fr =  new FileReader("aaa.txt");  
  13.         int ch;  
  14.         while((ch = fr.read()) != -1) {  
  15.             System.out.print((char)ch);  
  16.         }  
  17.         fr.close();  
  18.     }  
  19. }  
  20.   
  21. import java.io.FileWriter;  
  22. import java.io.IOException;  
  23.   
  24. public class Demo2_FileWriter {  
  25.   
  26.     /** 
  27.      * @param args 
  28.      * @throws IOException  
  29.      */  
  30.     public static void main(String[] args) throws IOException {  
  31.         FileWriter fw = new FileWriter("bbb.txt");  
  32.         fw.write("基础班快毕业了,革命尚未成功,大家继续努力");  
  33.         fw.close();  
  34.     }  
  35.   
  36. }  



3.什么情况下使用字符流

   字符流也可以拷贝文本文件,但不推荐使用. 因为读取时会把字节转为字符, 写出时还要把字符转回字节.

   程序需要读取一段文本, 或者需要写出一段文本的时候可以使用字符流

是否可以用字符流拷贝非纯文本的文件,例如音频,视频,图片?

    字符流只能操作纯文本的文件

[java]  view plain copy
  1. public static void main(String[] args) throws IOException {  
  2.         //demo1();  
  3.         FileReader fr = new FileReader("IO图片.png");  
  4.         FileWriter fw = new FileWriter("copy.png");  
  5.           
  6.         int ch;  
  7.         while((ch = fr.read()) != -1) {  
  8.             fw.write(ch);  
  9.         }  
  10.           
  11.         fr.close();  
  12.         fw.close();  
  13.     }  
  14.   
  15.     private static void demo1() throws FileNotFoundException, IOException {  
  16.         FileReader fr = new FileReader("day22笔记.txt");  
  17.         FileWriter fw = new FileWriter("copy.txt");  
  18.           
  19.         int ch;  
  20.         while((ch = fr.read()) != -1) {  
  21.             fw.write(ch);  
  22.         }  
  23.           
  24.         fr.close();  
  25.         fw.close();  
  26.     }  
  27.   
  28. }  


4.带缓冲的字符流

       BufferedReader的read()方法读取字符时会一次读取若干字符到缓冲区, 然后逐个返回给程序, 降低读取文件的次数, 提高效率

       BufferedWriter的write()方法写出字符时会先写到缓冲区, 缓冲区写满时才会写到文件, 降低写文件的次数, 提高效率

       BufferedReader的readLine()方法可以读取一行字符(不包含换行符号)

       BufferedWriter的newLine()可以输出一个跨平台的换行符号"\r\n"


[java]  view plain copy
  1. public class Demo5_Buffer {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      * flush和close的区别 
  7.      * flush方法用来刷出缓冲区的数据,刷完之后,还可以继续写 
  8.      * close方法是关闭流对象,在关之前会调用一次flush刷新缓冲区最后的内容,并将流关闭,关闭之后,不能再写 
  9.      */  
  10.     public static void main(String[] args) throws IOException {  
  11.         //demo1();  
  12.         //demo2();  
  13.         BufferedReader br = new BufferedReader(new FileReader("day22笔记.txt"));  
  14.         BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));  
  15.           
  16.         String line;  
  17.         while((line = br.readLine()) != null) {  
  18.             bw.write(line);  
  19.             //bw.write("\r\n");  
  20.             bw.newLine();  
  21.             /* 
  22.              * \r\n只支持的windows系统 
  23.              * newLine是跨平台的,支持任何操作系统 
  24.              */  
  25.         }  
  26.         br.close();  
  27.         bw.close();  
  28.     }  
  29. }  


5.LineNumberReader

       LineNumberReader是BufferedReader的子类, 具有相同的功能, 并且可以统计行号

       调用getLineNumber()方法可以获取当前行号

       调用setLineNumber()方法可以设置当前行号


[java]  view plain copy
  1. import java.io.FileNotFoundException;  
  2. import java.io.FileReader;  
  3. import java.io.IOException;  
  4. import java.io.LineNumberReader;  
  5.   
  6. public class Demo6_LineNumberReader {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      * @throws IOException  
  11.      */  
  12.     public static void main(String[] args) throws IOException {  
  13.         LineNumberReader lnr = new LineNumberReader(new FileReader("day22笔记.txt"));  
  14.         String line;  
  15.         lnr.setLineNumber(100);                                     //设置行号  
  16.         while((line = lnr.readLine()) != null) {  
  17.             System.out.println(lnr.getLineNumber() + ":" + line);   //获取行号  
  18.         }  
  19.           
  20.         lnr.close();  
  21.     }  
  22.   
  23. }  


6.使用指定的码表读取字符

       FileReader是使用默认码表读取文件, 如果需要使用指定码表读取, 那么可以使用InputStreamReader(字节流,编码表)

       FileWriter是使用默认码表写出文件, 如果需要使用指定码表写出, 那么可以使用OutputStreamWriter(字节流,编码表)


[java]  view plain copy
  1. import java.io.FileOutputStream;  
  2. import java.io.FileReader;  
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.OutputStreamWriter;  
  7. import java.io.UnsupportedEncodingException;  
  8.   
  9. public class Demo8_TransIO {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * 转换流 
  14.      * @throws IOException  
  15.      * 字符流(字节流+编码表) 
  16.      * 读gbk编码表的文件写到utf-8编码表文件 
  17.      * 高效 
  18.      */  
  19.     public static void main(String[] args) throws IOException {  
  20.           
  21.         BufferedReader br =   
  22.                 new BufferedReader(new InputStreamReader(new FileInputStream("day22笔记.txt"),"gbk"));  
  23.         BufferedWriter bw =  
  24.                 new BufferedWriter(new OutputStreamWriter(new FileOutputStream("UTF-8.txt"),"UTF-8"));  
  25.         int ch;  
  26.         while((ch = br.read()) != -1) {  
  27.             bw.write(ch);  
  28.         }  
  29.         br.close();  
  30.         bw.close();  
  31.     }  
  32. }  


7.装饰设计模式

     让被包装的对象变的更加强大


[java]  view plain copy
  1. public class Demo7_Wrap {  
  2.   
  3.       
  4.     public static void main(String[] args) {  
  5.         Student s = new Student();  
  6.         s.code();  
  7.         System.out.println("-------------------");  
  8.         ItcastStudent is = new ItcastStudent(s);  
  9.         is.code();  
  10.     }  
  11.   
  12. }  
  13.   
  14. interface Coder {  
  15.     public void code();  
  16. }  
  17.   
  18. class Student implements Coder {  
  19.     @Override  
  20.     public void code() {  
  21.         System.out.println("javase");  
  22.         System.out.println("javaweb");  
  23.     }  
  24. }  
  25.   
  26. class ItcastStudent implements Coder{  
  27.     private Student s;  
  28.     public ItcastStudent(Student s) {  
  29.         this.s = s;  
  30.     }  
  31.     @Override  
  32.     public void code() {  
  33.         s.code();  
  34.         System.out.println("数据库");  
  35.         System.out.println("安卓");  
  36.         System.out.println("ssh");  
  37.         System.out.println(".......");  
  38.     }  
  39. }  

二.序列流

    1.什么是序列流

       序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.

    2.使用方式

       整合两个: SequenceInputStream(InputStream, InputStream)

       整合多个: SequenceInputStream(Enumeration)


[java]  view plain copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.SequenceInputStream;  
  6. import java.util.Enumeration;  
  7. import java.util.Vector;  
  8.   
  9. public class Demo1_SequenceInputStream {  
  10.     public static void main(String[] args) throws IOException {  
  11.         demo1();  
  12.         demo2();  
  13.     //整合多个文件  
  14.         FileInputStream fis1 = new FileInputStream("a.txt");            //创建流对象,关联a.txt  
  15.         FileInputStream fis2 = new FileInputStream("b.txt");            //创建流对象,关联b.txt  
  16.         FileInputStream fis3 = new FileInputStream("c.txt");            //创建流对象,关联b.txt  
  17.         Vector<FileInputStream> v = new Vector<>();                     //创建集合对象  
  18.         v.add(fis1);                                                    //存储流对象  
  19.         v.add(fis2);  
  20.         v.add(fis3);  
  21.           
  22.         Enumeration<FileInputStream> en = v.elements();                   //获取流对象  
  23.         SequenceInputStream sis = new SequenceInputStream(en);          //传递给SequenceInputStream的构造函数  
  24.         FileOutputStream fos = new FileOutputStream("d.txt");           //创建流对象,关联c.txt  
  25.           
  26.         int b;  
  27.         while((b = sis.read()) != -1) {  
  28.             fos.write(b);  
  29.         }  
  30.           
  31.         sis.close();  
  32.         fos.close();  
  33.     }  
  34. //整合两个文件  
  35.     private static void demo2() throws FileNotFoundException, IOException {  
  36.         FileInputStream fis1 = new FileInputStream("a.txt");            //创建流对象,关联a.txt  
  37.         FileInputStream fis2 = new FileInputStream("b.txt");            //创建流对象,关联b.txt  
  38.         SequenceInputStream sis = new SequenceInputStream(fis1, fis2);  //创建流对象,把fis1和fis2整合成一个流对象  
  39.         FileOutputStream fos = new FileOutputStream("c.txt");           //创建流对象,关联c.txt  
  40.           
  41.         int b;  
  42.         while((b = sis.read()) != -1) {                                 //用SequenceInputStream读  
  43.             fos.write(b);                                               //写到c.txt  
  44.         }  
  45.           
  46.         sis.close();                                                    //关流  
  47.         fos.close();  
  48.     }  
  49.     //普通的读写多个文件  
  50.     private static void demo1() throws FileNotFoundException, IOException {  
  51.         FileInputStream fis1 = new FileInputStream("a.txt");  
  52.         FileInputStream fis2 = new FileInputStream("b.txt");  
  53.         FileOutputStream fos = new FileOutputStream("c.txt");  
  54.           
  55.         int b1;  
  56.         while((b1 = fis1.read()) != -1) {  
  57.             fos.write(b1);  
  58.         }  
  59.           
  60.         int b2;  
  61.         while((b2 = fis2.read()) != -1) {  
  62.             fos.write(b2);  
  63.         }  
  64.           
  65.         fis1.close();  
  66.         fis2.close();  
  67.         fos.close();  
  68.     }  
  69. }  

三.内存输出流

1.什么是内存输出流

       该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据

    2.使用方式

       创建对象: new ByteArrayOutputStream()

       写出数据: write(int), write(byte[])

       获取数据: toByteArray()

[java]  view plain copy
  1. public class Demo2_ByteArrayOutputStream {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      * 什么时候用ByteArrayOutputStream呢? 
  7.      * 当需要将内存当作缓冲区,而且是不断向内存中写数据的时候,比如发短信,聊qq,都是先将数据写到内存里 
  8.      * 再从内存将数据转换为字符串,写出 
  9.      */  
  10.     public static void main(String[] args) throws IOException {  
  11.         //demo1();  
  12.         FileInputStream fis = new FileInputStream("a.txt");  
  13.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  14.         int b;  
  15.         while((b = fis.read()) != -1) {  
  16.             baos.write(b);  
  17.         }  
  18.           
  19.         //byte[] arr = baos.toByteArray();                          //将缓冲区的内容赋值给byte数组  
  20.         //System.out.println(new String(arr));                      //将字节数组转换为字符串  
  21.         System.out.println(baos);                                   //默认调用toString方法  
  22.         //String str = baos.toString();  
  23.         fis.close();  
  24.     }  
  25. }         

四.对象操作流

1.什么是对象操作流

       该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作.

    2.使用方式

       写出: new ObjectOutputStream(OutputStream), writeObject()

       读取: new ObjectInputStream(InputStream), readObject()

    3.注意

       要写出的对象必须实现Serializable接口才能被序列化

(实现Serializable)

[java]  view plain copy
  1. import java.io.Serializable;  
  2.   
  3. public class Person implements Serializable {  
  4.     /** 
  5.      *  
  6.      */  
  7.     private static final long serialVersionUID = 3L;    //可以加也可以不加,加就是为了让大家更清楚的看懂异常  
  8.     private String name;  
  9.     private int age;  
  10.     public Person() {  
  11.         super();  
  12.           
  13.     }  
  14.     public Person(String name, int age) {  
  15.         super();  
  16.         this.name = name;  
  17.         this.age = age;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public int getAge() {  
  26.         return age;  
  27.     }  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.     @Override  
  32.     public String toString() {  
  33.         return "Person [name=" + name + ", age=" + age + "]";  
  34.     }  
  35.       
  36.       
  37. }  

(1)序列化,将对象写到文件上

[java]  view plain copy
  1. import cn.itcast.bean.Person;  
  2.   
  3. public class Demo3_ObjectOutputStream {  
  4.   
  5.        
  6.        
  7.     public static void main(String[] args) throws IOException {  
  8.         //demo1();    
  9.         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c.txt")); //创建输出流对象  
  10.         Person p1 = new Person("张三"23);                                               //创建人对象  
  11.         Person p2 = new Person("李四"24);  
  12.           
  13.         ArrayList<Person> list = new ArrayList<>();                                     //创建集合对象  
  14.         list.add(p1);                                                                   //将人添加到集合中  
  15.         list.add(p2);  
  16.         oos.writeObject(list);                                                          //写出集合对象  
  17.           
  18.         oos.close();                                                                    //关流  
  19.     }  
  20.   
  21.     private static void demo1() throws IOException, FileNotFoundException {  
  22.         Person p1 = new Person("张三"23);  
  23.         //FileOutputStream fos = new FileOutputStream("b.txt");  
  24.         //fos.write();                                          字节流不能写出对象  
  25.         //FileWriter fw = new FileWriter("b.txt");  
  26.         //fw.write(p1);                                         字符输出流不能写出对象  
  27.         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt")); //创建对象输出流,关联b.txt  
  28.         oos.writeObject(p1);                                                            //写出一个对象  
  29.         oos.close();  
  30.     }  
  31.   
  32. }  

(2)反序列化,将文件上数据读取出来

[java]  view plain copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5. import java.util.List;  
  6.   
  7. import cn.itcast.bean.Person;  
  8.   
  9. public class Demo4_ObjectInputStream {  
  10.   
  11.          * @param args  
  12.      @throws IOException   
  13.       @throws FileNotFoundException   
  14.       @throws ClassNotFoundException   
  15.           
  16.     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {  
  17.         //demo1();    
  18.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c.txt"));    //创建对象输入流对象,关联c.txt  
  19.         List<Person> list = (List<Person>) ois.readObject();                            //读取对象  
  20.         for (Person person : list) {                                                    //遍历集合  
  21.             System.out.println(person);  
  22.         }  
  23.           
  24.         ois.close();                                                                    //关流  
  25.     }  
  26.   
  27.     private static void demo1() throws IOException, FileNotFoundException,  
  28.             ClassNotFoundException {  
  29.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));    //创建对象输入流,关联b.txt  
  30.         Person p1 = (Person) ois.readObject();                                          //读取对象  
  31.         //Person p2 = (Person) ois.readObject();                                            //读取对象  
  32.         System.out.println(p1);                                                         //输出  
  33.         //System.out.println(p2);                                                           //输出  
  34.         ois.close();  
  35.     }  
  36.   
  37. }  

五.打印流

1.什么是打印流

       该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式

       System.out就是一个PrintStream, 其默认向控制台输出信息

    2.使用方式

       打印: print(), println()

       自动刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding) 

[java]  view plain copy
  1. import java.io.FileNotFoundException;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.PrintStream;  
  5. import java.io.PrintWriter;  
  6.   
  7. import cn.itcast.bean.Person;  
  8.   
  9. public class Demo5_PrintStream {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws IOException  
  14.      * PrintWriter是打印字符流,可以自动刷出缓冲区的内容 
  15.      * 但是只针对于println 
  16.      */  
  17.     public static void main(String[] args) throws IOException {  
  18.         demo1();  
  19.         demo2();  
  20.   
  21.         PrintStream ps = new PrintStream("e.txt");  
  22.         ps.println(97);         //将97转换成字符串写出  
  23.         ps.write(97);               //直接将97写出  
  24.         ps.close();  
  25.     }  
  26.   
  27.     private static void demo2() throws FileNotFoundException {  
  28. PrintWriter pw =  
  29.         new PrintWriter(newFileOutputStream("e.txt"),true);  
  30.         //pw.write("你好");  
  31.         //pw.close();  
  32.         pw.println("你好");  
  33.         //pw.print("你好");  
  34.         pw.close();  
  35.     }  
  36.   
  37.     private static void demo1() {  
  38.         PrintStream ps = System.out;  
  39.         ps.println(97);         //在打印97的时候,底层调用是Integer.toString(97)转换成字符串  
  40.         ps.println(new Person("张三",23));//当打印对象的时候,底层会调用String类里面valueOf方法  
  41.         Person p = null;        //valueOf方法会对对象做判断,如果是null就返回null,如果对象有具体的值  
  42.         ps.println(p);      //就调用该对象toString方法  
  43.     }  
  44.   
  45. }  

六.标准输入输出流

1.什么是标准输入输出流

       System.in是InputStream, 标准输入流, 默认可以从键盘输入读取字节数据

       System.out是PrintStream, 标准输出流, 默认可以向Console中输出字符和字节数据

    2.修改标准输入输出流

       修改输入流: System.setIn(InputStream)

       修改输出流: System.setOut(PrintStream)

[java]  view plain copy
  1. public class Demo6_SetInOut {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      */  
  7.     public static void main(String[] args) throws IOException {  
  8.           
  9.         System.setIn(new FileInputStream("day23笔记.txt"));   //改变标准输入流,指向的是day23笔记.txt  
  10.         System.setOut(new PrintStream("copy.txt")); //改变标准输出流,指向的是copy.txt  
  11.           
  12.         InputStream is = System.in; //获取输入流  
  13.         PrintStream ps = System.out;    //获取输出流  
  14.         int b;  
  15.         while((b = is.read()) != -1) {  //到文件上逐个字节读  
  16.             ps.write(b);            //将逐个字节写到文件上  
  17.         }  
  18.           
  19.         is.close();                                             //关流  
  20.         ps.close();  
  21.     }  
  22. }  

七.数据输入输出流

1.什么是数据输入输出流

       DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据

       例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.

    2.使用方式

       DataInputStream(InputStream), readInt(), readLong()

       DataOutputStream(OutputStream), writeInt(), writeLong()

[java]  view plain copy
  1. public class Demo7_DataOutputStream {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      * 00000000 00000000 00000011 11100101 
  7.      * 00000000 00000000 00000000 11100101 
  8.      */  
  9.     public static void main(String[] args) throws IOException {  
  10.         //demo1();  
  11.         DataOutputStream dos = new DataOutputStream(new FileOutputStream("e.txt"));  
  12.         dos.writeInt(997);                                      //写出一个int数  
  13.         dos.writeInt(998);  
  14.         dos.writeInt(999);  
  15.         dos.close();  
  16.     }  
  17.   
  18.     private static void demo1() throws FileNotFoundException, IOException {  
  19.         FileOutputStream fos = new FileOutputStream("e.txt");   //创建输出流对象,关联e.txt  
  20.         fos.write(997);                                         //写出一个字节,因为997超过byte的取值范围,所以会去掉前三个八位写出  
  21.         fos.write(998);  
  22.         fos.write(999);  
  23.         fos.close();  
  24.     }  
  25.   
  26. }  


[java]  view plain copy
  1. public class Demo8_DataInputStream {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      */  
  7.     public static void main(String[] args) throws IOException {  
  8.         //demo1();  
  9.         DataInputStream dis = new DataInputStream(new FileInputStream("e.txt"));  
  10.         int x = dis.readInt();  //读取一个int数,读取到的是写入文件的  
  11.         int y = dis.readInt();  
  12.         int z = dis.readInt();  
  13.         System.out.println(x);  
  14.         System.out.println(y);  
  15.         System.out.println(z);  
  16.         dis.close();  
  17.     }  
  18.   
  19.     private static void demo1() throws FileNotFoundException, IOException {  
  20.         FileInputStream fis = new FileInputStream("e.txt");  
  21.         int x = fis.read();         //读取到的不是原先写入的数据  
  22.         int y = fis.read();  
  23.         int z = fis.read();  
  24.         System.out.println(x);  
  25.         System.out.println(y);  
  26.         System.out.println(z);  
  27.         fis.close();  
  28.     }  
  29.   
  30. }  

八.Properties

1.向内存中存入值,并通过键获取值setProperty(key,value) getProperty(key);

    2.通过load方法,读取配置文件,propertyNames获取所有的key,返回Enumeration

    3.根据键改值,并重新存入到配置文件setProperty(key,value),list(newPrintStream())

    System.getProperties();获取系统属性,propertyNames将所有的键返回到枚举里,就可以迭代了

 

[java]  view plain copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.PrintStream;  
  5. import java.util.Enumeration;  
  6. import java.util.Properties;  
  7.   
  8. public class Demo9_Properties {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      * @throws IOException  
  13.      * @throws FileNotFoundException  
  14.      */  
  15.     public static void main(String[] args) throws FileNotFoundException, IOException {  
  16.         demo1();  
  17.         Properties prop = new Properties();  
  18.         prop.load(new FileInputStream("config.properties"));        //通过load方法可以将配置文件的信息读取  
  19.         prop.setProperty("username""lisi");  
  20.         prop.list(new PrintStream("config.properties"));            //通过list方法将集合中的内容写回配置文件  
  21.         System.out.println(prop);  
  22.     }  
  23.   
  24.     private static void demo1() {  
  25.         Properties prop = new Properties();  
  26.         prop.setProperty("username""张三");  
  27.         prop.setProperty("password""12345");  
  28.         prop.setProperty("qq""54321");  
  29.         prop.setProperty("tel""18987654321");  
  30.           
  31.         //System.out.println(prop);  
  32.         Enumeration<?> en = prop.propertyNames();  
  33.         while(en.hasMoreElements()) {  
  34.             String key = (String) en.nextElement();  
  35.             String value = prop.getProperty(key);  
  36.             System.out.println(key + "=" +value);  
  37.         }  
  38.     }  
  39.   
  40. }  

九.IO总结

1.字节流

       FileInputStream, FileOutputStream, 自定义数组拷贝文件

       BufferedInputStream, BufferedOutputStream, 内置缓冲区拷贝文件

    2.字符流

       FileReader, FileWriter

       InputStreamReader, OutputStreamWriter

       BufferedReader, BufferedWriter

       会读写各种码表的文件中的文本数据



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值