当前位置: 首页 > news >正文

黑马程序员_properties,打印流,合并流,分割流

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


1. Properties

1.概述

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

2.常用方法

 public class PropertiesDemo1 {
   public static void main(String[] args) {
     Properties pro = new Properties();
     /* setProperty(String key,String value)添加键和值 */
     pro.setProperty("java01", "001");
     pro.setProperty("java02", "002");
     /* 通过键获得值:String getProperty(String key) */
     System.out.println(pro.getProperty("java01"));
     /* Set<String>stringPropertyNames()获得键集 */
     Set<String> set = pro.stringPropertyNames();
     for (String s : set)
        System.out.println(s + ":" + pro.getProperty(s));
   }
 
}
结果:
001
java02:002
java01:001

3.配置文件

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

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


import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
 
public class PropertiesDemo1 {
   public static void main(String[] args) throws IOException {
     BufferedReader br = new BufferedReader(new FileReader("F:\\pro.txt"));//读取流
     HashMap<String, String> map = new HashMap<String, String>();//map集合
     String line = null;
     while ((line = br.readLine()) != null) {
        String[] s = line.split("=");
        map.put(s[0], s[1]);
     }
     System.out.println(map);
   }
 
}
结果:
{java02=002, java03=003, java01=001}


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

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
 
public class PropertiesDemo1 {
   public static void main(String[] args) throws IOException {
     /* void load(Reader reader)将读取流字符流加载到集合中 */
     Properties pro = new Properties();
     FileReader fr = new FileReader("F:\\pro.txt");
     pro.load(fr);// 将字符读取流中读取的文件放到Properties对象中
     System.out.println("加载后的集合:" + pro);
     /* 下面我们修改集合中的数值 */
     pro.setProperty("java02", "hello");
     /*
      * store(Writer writer,String
      * comments)通过字符写入流,把集合中的信息更新配置文件,comments是注视内容
      */
     FileWriter fw = new FileWriter("F:\\pro.txt");
     pro.store(fw, "java");// 更新配置文件,注释为:java
     fr.close();
     fw.close();
   }
 
}
结果:
加载后的集合:{java03=003, java02=002, java01=001}


4.计算程序运行次数


 importjava.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;
 
public class PropertiesDemo {
 
      publicstatic void main(String[] args) throws IOException {
           /*
            * 计算程序的运行次数 思路: 1.创建一个配置文件,然后然后里面定义了一个计数器
            * 2.每次运行的时候,都取出来,然后自增,然后再存回去
            */
 
           Filefile = new File("F:\\pro.ini");
           if(!file.exists())
                 file.createNewFile();
 
           Propertiespro = new Properties();
           pro.load(newFileInputStream(file));// 加载流
           Stringvalue = pro.getProperty("count");
           intcount = 0;
           if(value != null)
                 count= Integer.parseInt(value);
           count++;//自增
           pro.setProperty("count",count + "");// 改变值
           pro.store(newFileOutputStream(file), "java for count");// 更新文件
           System.out.println(pro);
      }
 
}结果:
运行一次,count就+1.

2. 打印流

PrintStream:字节打印流

其构造方法传入的参数:

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

PrintWriter:字符打印流

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

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

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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
 
public class PrintStreamDemo {
   public static void main(String[] agrs) throws IOException {
     // File f=newFile("F:\\demo.txt");
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     PrintWriter pw = new PrintWriter(System.out,true);//true表示自动刷新,但是必须是println方法
     String line = null;
     while ((line = br.readLine()) != null) {
        if ("over".equals(line))
          return;
        pw.println("内容是:" + line);
        //pw.flush();
 
     }
     br.close();
     pw.close();
   }
}


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

3. 合并流

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

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

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

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

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.io.FileInputStream;
 
public class SqueDemo {
  public static void main(String[] agrs) throws IOException {
    Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(new FileInputStream("F:\\1.txt"));
    v.add(new FileInputStream("F:\\2.txt"));
    v.add(new FileInputStream("F:\\3.txt"));
    Enumeration<FileInputStream> en = v.elements();
    SequenceInputStreamsis = new SequenceInputStream(en);
 
    FileOutputStream out = new FileOutputStream("F:\\4.txt");
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
      out.write(b, 0, len);
      out.flush();
    }
    sis.close();
    out.close();
 
  }
}


4. 切割流

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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
 
public class PhotoDemo {
  public static void main(String[] args) throws IOException {
 
    FenGe();
 
    HeBing();
 
  }
 
  private static void HeBing() throws FileNotFoundException, IOException {
    /* 其实这个也可以使用循环,然后添加到集合, */
    Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(new FileInputStream("F:\\part\\1.part"));
    v.add(new FileInputStream("F:\\part\\2.part"));
    v.add(new FileInputStream("F:\\part\\3.part"));
    v.add(new FileInputStream("F:\\part\\4.part"));
    v.add(new FileInputStream("F:\\part\\5.part"));
    Enumeration<FileInputStream> en = v.elements();
    SequenceInputStream sis = new SequenceInputStream(en);
 
    FileOutputStream out = new FileOutputStream("F:\\part\\1.bmp");
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
      out.write(b, 0, len);
      out.flush();
    }
    sis.close();
    out.close();
  }
 
  /* 分割 */
  private static void FenGe() throws FileNotFoundException, IOException {
    FileInputStream input = new FileInputStream("F:\\1.png");
 
    FileOutputStream out = null;
    byte[] buf = new byte[1024 * 100];
    int count = 1;
    int len = 0;
    while ((len = input.read(buf)) != -1) {
      out = new FileOutputStream("F:\\part\\" + (count++) + ".part");// 分割后的后缀名自己可以自定义
      out.write(buf, 0, len);
      out.flush();
      out.close();
 
    }
    input.close();
  }
 
}



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



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


相关文章:

  • 软件架构师的12项修炼_读书纪要_P3商务技能修炼
  • 内存对齐规则
  • 【Deep Learning学习笔记】Deep learning for nlp without magic_Bengio_ppt_acl2012
  • 不用外部JAR包,自己实现JSP文件上传!
  • 计算机技术不是吓唬大众的工具!
  • Windows程序设计学习笔记--第一个Windows程序以及宽字符集(了解)
  • 云计算和大数据入门
  • Windows Azure使用必读
  • Windows Azure新功能:Hadoop和Web版的移动服务
  • 云计算的理解
  • 微软云存储SkyDrive API:将你的数据连接到任何应用、任何平台,及任何设备上...
  • 在 Windows Azure 上部署预配置 Oracle VM
  • 对于Visual Studio 2013中 Blend的HTML开发人员来说什么是新的
  • 微软云服务获政府认证 云服务或将迎来黄金期
  • SAE 搭建 WordPress
  • [译]Python中的类属性与实例属性的区别
  • ECS应用管理最佳实践
  • isset在php5.6-和php7.0+的一些差异
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • maven工程打包jar以及java jar命令的classpath使用
  • MYSQL 的 IF 函数
  • nodejs实现webservice问题总结
  • Web标准制定过程
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 关于extract.autodesk.io的一些说明
  • 关于Java中分层中遇到的一些问题
  • 坑!为什么View.startAnimation不起作用?
  • 前端相关框架总和
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • (arch)linux 转换文件编码格式
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (python)数据结构---字典
  • (TOJ2804)Even? Odd?
  • (八)Flask之app.route装饰器函数的参数
  • (二)springcloud实战之config配置中心
  • (十六)Flask之蓝图
  • (十一)图像的罗伯特梯度锐化
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • .gitignore文件_Git:.gitignore
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net core使用ef 6
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .Net接口调试与案例
  • @FeignClient注解,fallback和fallbackFactory
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [Android View] 可绘制形状 (Shape Xml)
  • [AutoSar]BSW_Memory_Stack_004 创建一个简单NV block并调试
  • [BJDCTF2020]The mystery of ip1
  • [C++] 默认构造函数、参数化构造函数、拷贝构造函数、移动构造函数及其使用案例
  • [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  • [Foreman]解决Unable to find internal system admin account
  • [Geek Challenge 2023] web题解