A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© IT_JM 中级黑马   /  2013-10-11 12:13  /  1404 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Properties类
一、概述
1、Properties是Hashtable的子类,它具备Map集合的特点。而且它里面还有存储的键值对,都是字符串,无泛型定义。是集合中和IO技术想结合的集合容器。
2、特点:
        1)可用于键值对形式的配置文件
        2)在加载时,需要数据有固定的格式,常用的是:键=值
二、特有方法
1、设置
        Object setProperty(String key,String value);
        //设置键和值,调用Hashtable的方法put
2、获取
        String getProperty(String key);
        //指定key搜索value
        Set<String> stringPropertyName();
        //返回属性列表的键集,存入Set集合
3、加载流和存入流
        void load(InputStream ism);
        //从输入字节流中读取属性列表(键和元素对)。又称将流中的数据加载进集合。
        void load(Readerreader);
        //从输入字符流中读取属性列表(键和元素对)。又称将流中的数据加载进集合。
        voidlist(PrintStream out);//将属性列表输出到指定的输出流
        void store(OutputStreamout,String comments);
        //对应load(InputStream )将属性列表(键值对)写入输出流。comments属性列表的描述。
        void store(Writerwriter, String comments);
        //对应load(Reader)将属性列表(键值对)写入输出流。comments属性列表的描述。
代码示例:
  1. 1. /*
  2. 2. 练习:用于记录应用程序运行次数。如果使用次数已到,那么给出注册提示。
  3. 3.
  4. 4. 分析:
  5. 5. 很容易想到的是:计数器。可是该计数器定义在程序中,随着该应用程序的退出,该计数器也在内存中消失了。
  6. 6. 所以要建立一个配置文件,用于记录该软件的使用次数。该配置文件使用键值对的形式。键值对数据是map集合。数据是以文件形式存储。使用io技术。那么map+io——>Properties。
  7. 7.
  8. 8. 思路:1、用读取流关联文本信息文件。如果存在则读取,如果不存在,则创建
  9. 9. 2、每次运行,将文件数据存入集合中,读取值,判断次数,如果小于等于5次,则次数增加1次,如果大于则输出提示信息。
  10. 10. 3、将值小于等于5次的信息数据存入文件中
  11. 11. */
  12. 12. import java.util.*;
  13. 13. import java.io.*;
  14. 14.
  15. 15. class RunCount
  16. 16. {
  17. 17. public static void main(String[] args)throws IOException
  18. 18. {
  19. 19. int count=runCount();
  20. 20. if(count>5)//如果程序被使用了超过5次,则终止使用,并提示
  21. 21. {
  22. 22. System.out.println("次数到了,交钱!!!!!");
  23. 23. return ;
  24. 24. }
  25. 25. else
  26. 26. System.out.println("程序第"+count+"次Run!");
  27. 27. }
  28. 28. //获取程序运行的次数
  29. 29. public static int runCount()throws IOException
  30. 30. {
  31. 31. Properties ps=new Properties();//创建集合对象
  32. 32.
  33. 33. File file=new File("info.ini");//将文件进行封装
  34. 34. if(!file.exists())//判断是否存在
  35. 35. file.createNewFile();
  36. 36. FileReader fr=new FileReader(file);//将文件于读取流进行关联
  37. 37.
  38. 38. ps.load(fr);//加载流中的文件数据到集合中
  39. 39.
  40. 40. int count=0;//定义计数器
  41. 41. String value=ps.getProperty("time");//获取次数值
  42. 42.
  43. 43. if(value!=null)//如过值不等于null,则将其赋值给count
  44. 44. {
  45. 45. count=Integer.parseInt(value);
  46. 46. }
  47. 47. count++;//每启动一次自增
  48. 48. ps.setProperty("time",count+"");//将次数记录住集合
  49. 49.
  50. 50. FileWriter fw=new FileWriter(file);
  51. 51. ps.store(fw,"");//将集合中的数据存入硬盘文件中
  52. 52.
  53. 53. fr.close();//关流
  54. 54. fw.close();
  55. 55.
  56. 56. return count;//返回程序启动的次数
  57. 57. }
  58. 58. }
复制代码
    管道流
一、概述
1、管道流:PipedInputStream和PipedOutputStream
2、特点:
         a、输入输出可以直接进行连接,不用再借助数组或集合等容器进行临时存储。     
        b、一般结合多线程使用。通常,数据由某个线程写入PipedOutputStream对象,并由其他线程从连接的 PipedInputStream 读取。
二、常见操作步骤
        1、要先创建一个读和写的两个类,实现Runnable接口,因为是两个不同的线程,覆盖run方法,注意,需要在内部处理异常。
        2、创建两个管道流,并用connect()方法将两个流连接
        3、创建读写对象,并传入两个线程内,并start执行。
代码示例
  1. 1. import java.io.*;
  2. 2. //读取线程
  3. 3. class Read implements Runnable
  4. 4. {
  5. 5. private PipedInputStream in;
  6. 6. Read(PipedInputStream in)
  7. 7. {
  8. 8. this.in=in;
  9. 9. }
  10. 10. //覆盖run方法
  11. 11. public void run()
  12. 12. {
  13. 13. try
  14. 14. {
  15. 15. //用来存储读到的字节
  16. 16. byte[] by=new byte[1024];
  17. 17. System.out.println("读取前。。没有数据阻塞");
  18. 18. //读取流中数据
  19. 19. int len=in.read(by);
  20. 20. System.out.println("读到数据。。阻塞结束");
  21. 21.
  22. 22. //将字节数组转换为字符串打印输出
  23. 23. String s=new String(by,0,len);
  24. 24.
  25. 25. System.out.println(s);
  26. 26. }
  27. 27. catch (IOException e)
  28. 28. {
  29. 29. throw new RuntimeException("读取数据失败");
  30. 30. }
  31. 31. finally
  32. 32. {
  33. 33. try
  34. 34. {
  35. 35. if(in!=null)
  36. 36. in.close();
  37. 37. }
  38. 38. catch (IOException e)
  39. 39. {
  40. 40. throw new RuntimeException("流关闭失败");
  41. 41. }
  42. 42. }
  43. 43. }
  44. 44. }
  45. 45.
  46. 46. //写线程
  47. 47. class Write implements Runnable
  48. 48. {
  49. 49. private PipedOutputStream out;
  50. 50. Write(PipedOutputStream out)
  51. 51. {
  52. 52. this.out=out;
  53. 53. }
  54. 54. //覆盖run方法
  55. 55. public void run()
  56. 56. {
  57. 57. try
  58. 58. {
  59. 59. System.out.println("开始写入数据,等待3秒后。");
  60. 60. Thread.sleep(3000);
  61. 61. //写入数据到管道流中
  62. 62. out.write("piped shi shem ma?".getBytes());
  63. 63. }
  64. 64. catch (Exception e)
  65. 65. {
  66. 66. throw new RuntimeException("写入数据失败");
  67. 67. }
  68. 68. finally
  69. 69. {
  70. 70. try
  71. 71. {
  72. 72. if(out!=null)
  73. 73. out.close();
  74. 74. }
  75. 75. catch (IOException e)
  76. 76. {
  77. 77. throw new RuntimeException("流关闭失败");
  78. 78. }
  79. 79. }
  80. 80. }
  81. 81. }
  82. 82.
  83. 83. class PipedStreamDemo
  84. 84. {
  85. 85. public static void main(String[] args)
  86. 86. {
  87. 87. try
  88. 88. {
  89. 89. //创建管道流对象,并关联
  90. 90. PipedInputStream in=new PipedInputStream();
  91. 91. PipedOutputStream out=new PipedOutputStream();
  92. 92. in.connect(out);
  93. 93.
  94. 94. //启动线程
  95. 95. new Thread(new Read(in)).start();
  96. 96. new Thread(new Write(out)).start();
  97. 97. }
  98. 98. catch (IOException e)
  99. 99. {
  100. 100. throw new RuntimeException("管道流关联失败");
  101. 101. }
  102. 102. }
  103. 103. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄文伯 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马