黑马程序员_<<properties,打印流,合并流,分割流>>

来源:互联网 发布:mac刷新dns 编辑:程序博客网 时间:2024/05/16 12:07

分类: IO流 256人阅读 评论(0) 收藏 举报
Properties打印流分割流合并流黑马程序员

目录(?)[+]

--------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流! --------------------


1. Properties

         1.概述

Properties是Hashtable的子类,存储的格式那么也是键值对,但是键和值都是字符串类型

        2.常用方法

      

[java] view plaincopy
  1.  public class PropertiesDemo1 {  
  2.    public static void main(String[] args) {  
  3.      Properties pro = new Properties();  
  4.      /* setProperty(String key,String value)添加键和值 */  
  5.      pro.setProperty("java01""001");  
  6.      pro.setProperty("java02""002");  
  7.      /* 通过键获得值:String getProperty(String key) */  
  8.      System.out.println(pro.getProperty("java01"));  
  9.      /* Set<String>stringPropertyNames()获得键集 */  
  10.      Set<String> set = pro.stringPropertyNames();  
  11.      for (String s : set)  
  12.         System.out.println(s + ":" + pro.getProperty(s));  
  13.    }  
  14.    
  15. }  
  16. 结果:  
  17. 001  
  18. java02:002  
  19. java01:001  

            3.配置文件

将配置文件中的信息存到集合中,然后修改其键和值,然后在传给文件。配置文件中的信息都是用=号存储的,例如:张三=001

第一种方法:我们不使用Properties对象,就是用集合来操作,思路:我们将文件读取出来,然后对每一行进行用=分割,把左边的存为键,把右边的存为值。


[java] view plaincopy
  1. import java.io.FileReader;  
  2. import java.io.IOException;  
  3. import java.util.HashMap;  
  4. import java.util.Properties;  
  5. import java.util.Set;  
  6.    
  7. public class PropertiesDemo1 {  
  8.    public static void main(String[] args) throws IOException {  
  9.      BufferedReader br = new BufferedReader(new FileReader("F:\\pro.txt"));//读取流  
  10.      HashMap<String, String> map = new HashMap<String, String>();//map集合  
  11.      String line = null;  
  12.      while ((line = br.readLine()) != null) {  
  13.         String[] s = line.split("=");  
  14.         map.put(s[0], s[1]);  
  15.      }  
  16.      System.out.println(map);  
  17.    }  
  18.    
  19. }  
  20. 结果:  
  21. {java02=002, java03=003, java01=001}  


第二种方法:我们使用Properties对象,这样我们可以方便的加载流,来操作文件。

 

[java] view plaincopy
  1. import java.io.FileReader;  
  2. import java.io.FileWriter;  
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.Properties;  
  6. import java.util.Set;  
  7.    
  8. public class PropertiesDemo1 {  
  9.    public static void main(String[] args) throws IOException {  
  10.      /* void load(Reader reader)将读取流字符流加载到集合中 */  
  11.      Properties pro = new Properties();  
  12.      FileReader fr = new FileReader("F:\\pro.txt");  
  13.      pro.load(fr);// 将字符读取流中读取的文件放到Properties对象中  
  14.      System.out.println("加载后的集合:" + pro);  
  15.      /* 下面我们修改集合中的数值 */  
  16.      pro.setProperty("java02""hello");  
  17.      /* 
  18.       * store(Writer writer,String 
  19.       * comments)通过字符写入流,把集合中的信息更新配置文件,comments是注视内容 
  20.       */  
  21.      FileWriter fw = new FileWriter("F:\\pro.txt");  
  22.      pro.store(fw, "java");// 更新配置文件,注释为:java  
  23.      fr.close();  
  24.      fw.close();  
  25.    }  
  26.    
  27. }  
  28. 结果:  
  29. 加载后的集合:{java03=003, java02=002, java01=001}  


          4.计算程序运行次数


 

[java] view plaincopy
  1.  importjava.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.io.PrintStream;  
  8. import java.util.Properties;  
  9.    
  10. public class PropertiesDemo {  
  11.    
  12.       publicstatic void main(String[] args) throws IOException {  
  13.            /* 
  14.             * 计算程序的运行次数 思路: 1.创建一个配置文件,然后然后里面定义了一个计数器 
  15.             * 2.每次运行的时候,都取出来,然后自增,然后再存回去 
  16.             */  
  17.    
  18.            Filefile = new File("F:\\pro.ini");  
  19.            if(!file.exists())  
  20.                  file.createNewFile();  
  21.    
  22.            Propertiespro = new Properties();  
  23.            pro.load(newFileInputStream(file));// 加载流  
  24.            Stringvalue = pro.getProperty("count");  
  25.            intcount = 0;  
  26.            if(value != null)  
  27.                  count= Integer.parseInt(value);  
  28.            count++;//自增  
  29.            pro.setProperty("count",count + "");// 改变值  
  30.            pro.store(newFileOutputStream(file), "java for count");// 更新文件  
  31.            System.out.println(pro);  
  32.       }  
  33.    
  34. }结果:  
  35. 运行一次,count就+1.  

2. 打印流

     PrintStream:字节打印流

          其构造方法传入的参数:

                File对象,字符串路径,字节流。

     PrintWriter:字符打印流

          其构造方法传入的参数有:

              File对象,字符串路径,字节流,字符流。

     另外注意的是:字符流中传入的是流的话,可以设置自动刷新,只有使用println,printf,format方法可以自动刷新。

 

[java] view plaincopy
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.PrintWriter;  
  6.    
  7. public class PrintStreamDemo {  
  8.    public static void main(String[] agrs) throws IOException {  
  9.      // File f=newFile("F:\\demo.txt");  
  10.      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  11.      PrintWriter pw = new PrintWriter(System.out,true);//true表示自动刷新,但是必须是println方法  
  12.      String line = null;  
  13.      while ((line = br.readLine()) != null) {  
  14.         if ("over".equals(line))  
  15.           return;  
  16.         pw.println("内容是:" + line);  
  17.         //pw.flush();  
  18.    
  19.      }  
  20.      br.close();  
  21.      pw.close();  
  22.    }  
  23. }  


现在是把内容打印在了控制台上,也可以打印在文件中,把PrintWriter流对象更改一下就可以的啊。

3. 合并流

          将多个流合并成一个流,便于操作

  例如:三个文件的内容写入到第四个文件中,那先把指定到前三个文件的流合并成一个流,然后指向第四个进行读取后,写入

     1.可以现将两个流合并到一个流,然后再把合并流和另外一个流再合并,public SequenceInputStream(InputStreams1,InputStream s2)

     2.也可以使用集合,public SequenceInputStream(Enumeration<? extends InputStream> e)

 

[java] view plaincopy
  1. import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.SequenceInputStream;  
  4. import java.util.Enumeration;  
  5. import java.util.Vector;  
  6. import java.io.FileInputStream;  
  7.    
  8. public class SqueDemo {  
  9.   public static void main(String[] agrs) throws IOException {  
  10.     Vector<FileInputStream> v = new Vector<FileInputStream>();  
  11.     v.add(new FileInputStream("F:\\1.txt"));  
  12.     v.add(new FileInputStream("F:\\2.txt"));  
  13.     v.add(new FileInputStream("F:\\3.txt"));  
  14.     Enumeration<FileInputStream> en = v.elements();  
  15.     SequenceInputStreamsis = new SequenceInputStream(en);  
  16.    
  17.     FileOutputStream out = new FileOutputStream("F:\\4.txt");  
  18.     byte[] b = new byte[1024];  
  19.     int len = 0;  
  20.     while ((len = sis.read(b)) != -1) {  
  21.       out.write(b, 0, len);  
  22.       out.flush();  
  23.     }  
  24.     sis.close();  
  25.     out.close();  
  26.    
  27.   }  
  28. }  


 

4. 切割流

将图片分割后,然后在合并

 

[java] view plaincopy
  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 PhotoDemo {  
  10.   public static void main(String[] args) throws IOException {  
  11.    
  12.     FenGe();  
  13.    
  14.     HeBing();  
  15.    
  16.   }  
  17.    
  18.   private static void HeBing() throws FileNotFoundException, IOException {  
  19.     /* 其实这个也可以使用循环,然后添加到集合, */  
  20.     Vector<FileInputStream> v = new Vector<FileInputStream>();  
  21.     v.add(new FileInputStream("F:\\part\\1.part"));  
  22.     v.add(new FileInputStream("F:\\part\\2.part"));  
  23.     v.add(new FileInputStream("F:\\part\\3.part"));  
  24.     v.add(new FileInputStream("F:\\part\\4.part"));  
  25.     v.add(new FileInputStream("F:\\part\\5.part"));  
  26.     Enumeration<FileInputStream> en = v.elements();  
  27.     SequenceInputStream sis = new SequenceInputStream(en);  
  28.    
  29.     FileOutputStream out = new FileOutputStream("F:\\part\\1.bmp");  
  30.     byte[] b = new byte[1024];  
  31.     int len = 0;  
  32.     while ((len = sis.read(b)) != -1) {  
  33.       out.write(b, 0, len);  
  34.       out.flush();  
  35.     }  
  36.     sis.close();  
  37.     out.close();  
  38.   }  
  39.    
  40.   /* 分割 */  
  41.   private static void FenGe() throws FileNotFoundException, IOException {  
  42.     FileInputStream input = new FileInputStream("F:\\1.png");  
  43.    
  44.     FileOutputStream out = null;  
  45.     byte[] buf = new byte[1024 * 100];  
  46.     int count = 1;  
  47.     int len = 0;  
  48.     while ((len = input.read(buf)) != -1) {  
  49.       out = new FileOutputStream("F:\\part\\" + (count++) + ".part");// 分割后的后缀名自己可以自定义  
  50.       out.write(buf, 0, len);  
  51.       out.flush();  
  52.       out.close();  
  53.    
  54.     }  
  55.     input.close();  
  56.   }  
  57.    
  58. }  



其实也可以分割媒体,电影和音乐的。

原创粉丝点击